以角度下载的损坏的zip文件

时间:2021-09-10 09:56:13

Angular client code:

角度客户端代码:

         $http.post('/zip', {
            id: _id
        })
        .success(function (data, status, headers, config) {
            var blob = new Blob([data], {type: "application/zip"});
            var contentDisp = headers('content-disposition');
            if (contentDisp && /^attachment/i.test(contentDisp)) {
                var fileName = contentDisp.toLowerCase()
                    .split('filename=')[1]
                    .split(';')[0]
                    .replace(/"/g, '');
                //The below command works but generates a corrupt zip file.
                FileSaver.saveAs(blob, fileName);
            }
        })
        .error(function () {
            console.log("Could not download");
        });

NodeJS Server Code:

NodeJS服务器代码:

        app.route('/zip/')
        .post(function(req, res) {

          var output = fs.createWriteStream(join(outdir, outzipfile));
          //Using s3zip to archive.
          s3Zip
           .archive({ s3: s3Client, bucket: bucket}, folder, s3_files)
           .pipe(output);

          output.on('close', function() {
             //This sends back a zip file.
             res.download(outPutDirectory + outputBcemFile);
          });

          output.on('error', function(err) {
             console.log(err);
             return res.status(500).json({error: "Internal Server Error"});
          });

        });

Although FileSaver.saveAs works and downloads the zip file, it seems to be corrupted. Is the type "application/zip" correct? I have also tried "octet/stream" and it downloads a corrupt zip file too. Any help would be highly invaluable! Thanks.

虽然FileSaver.saveAs工作并下载zip文件,但它似乎已损坏。 “application / zip”类型是否正确?我也试过“octet / stream”,它也下载了一个损坏的zip文件。任何帮助都将非常宝贵!谢谢。

1 个解决方案

#1


2  

This is a bug mentioned in Git in below link :

这是下面链接中Git中提到的错误:

https://github.com/eligrey/FileSaver.js/issues/156

https://github.com/eligrey/FileSaver.js/issues/156

To solve you need to add : responseType: 'arraybuffer' to your $http Request headers and it will work.

要解决此问题,您需要在$ http请求标头中添加:responseType:'arraybuffer',它将起作用。

#1


2  

This is a bug mentioned in Git in below link :

这是下面链接中Git中提到的错误:

https://github.com/eligrey/FileSaver.js/issues/156

https://github.com/eligrey/FileSaver.js/issues/156

To solve you need to add : responseType: 'arraybuffer' to your $http Request headers and it will work.

要解决此问题,您需要在$ http请求标头中添加:responseType:'arraybuffer',它将起作用。