简体   繁体   English

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

[英]how to save octet stream to zip in javascript?

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);
                }
            });

In above code am getting a response in octet-stream format am reading this stream reader.read().then((value, done) like this. in value i have Uint8Array. how can I save this value as a zip file? 在上面的代码中以八位字节流格式获得响应,正在读取此流reader.read().then((value, done)像这样。在值中我有Uint8Array。如何将该值另存为zip文件?

You can do something like this: 您可以执行以下操作:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM