如何将八位位组流保存为javascript中的zip?

时间:2018-09-21 12:25:15

标签: javascript node.js ajax asp.net-web-api filesaver.js

return new Promise((resolve) => {
    return fetch('http:\\localhost:8080\downloadzipurl', {
        method: 'POST',
        headers: {
            Authorization: authKey,
            'Content-Type': 'application/json',
            'cache-control': 'no-cache',
            'Access-Control-Allow-Origin': '*',
        },
        body: JSON.stringify({
                  userName,
                  providerId,
                  sfgFEVersion,
                }),
    })
      .then((resp) => {
        if (resp.status >= 200 && resp.status < 300) {
            const reader = resp.body.getReader();
            const pump = () => reader.read().then((value, done) => { 
                if (done) {
                    //writer.close();
                    console.log('Api returned null response');
                } else {
                    console.log(value);
                    const blob = new Blob([value], { type: 'application/zip'});
                    const fileName = 'QCPReport.zip';
                    FileSaver.saveAs(blob, fileName);
                }
            });

在上面的代码中,八位字节流格式的响应正在读取该流reader.read().then((value, done),如下所示。在值上我有Uint8Array。如何将该值另存为zip文件?

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

const blob = new Blob([value]);

const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.target = '_blank';
link.setAttribute("type", "hidden");

// This is needed for firefox
document.body.appendChild(link);

link.click();
link.remove();