代码之家  ›  专栏  ›  技术社区  ›  CTOverton

带有jszip javascript/jszip升级的xhr post

  •  1
  • CTOverton  · 技术社区  · 6 年前

    我试图用一个zip文件来“发布”和xhr请求,但是我无法以正确的格式获取zip。我发现一个帖子显示了一个类似的请求:

    var zip = new JSZip(); // create the jszip zip
    var input = $("#image")[0]; // Get the image from dom (image is an input button)
    zip.file("test.png", input.files[0], {base64: true}); // Add uploaded image to zip
    
    var content = zip.generate({type:"blob"}); // Format zip to blob
    
    //prepare file for api call
    var data = new FormData();
    data.append("files", content, "Test.zip");
    

    首先要做的事, zip.generate({type:"blob"}); 已弃用。升级指南指出:

    // 2.x
    zip.generate();
    // 3.x
    zip.generateAsync({type:"uint8array"})
    .then(function (content) {
        // use content
    });
    

    我不明白“使用内容”是什么。如果我把那个函数留空,代码就不会运行。我会列出错误,但我使用的是sap web ide,它不会运行它,也不会显示错误。

    如何格式化ZIP以满足XHR请求?


    有用的链接:

    1 回复  |  直到 6 年前
        1
  •  1
  •   Asif Ali    6 年前

    你为什么不试试这样

    zip.generateAsync({type:"blob"}).then(function(content) {
      var data = new FormData();
      data.append("files", content, "Test.zip");
    });
    

    在这里查一下 https://github.com/Stuk/jszip