使用Express和Archiver将Zip文件从服务器发送到客户端浏览器

use*_*354 5 zip node.js express node-archiver angular

我是Node的初学者,我试图弄清楚如何在服务器上创建一个zip文件,然后将其发送到客户端,然后将该zip文件下载到用户的浏览器。我正在使用Express框架,并且正在使用Archiver进行压缩。以下是我的服务器代码,摘自动态创建并将zip流传输到客户端

router.get('/image-dl', function (req,res){

    res.writeHead(200, {
        'Content-Type': 'application/zip',
        'Content-disposition': 'attachment; filename=myFile.zip'
    });

    var zip = archiver('zip');

    // Send the file to the page output.
    zip.pipe(res);

    // Create zip with some files. Two dynamic, one static. Put #2 in a sub folder.
    zip.append('Some text to go in file 1.', { name: '1.txt' })
        .append('Some text to go in file 2. I go in a folder!', { name: 'somefolder/2.txt' })
        .finalize();
});
Run Code Online (Sandbox Code Playgroud)

因此,它压缩两个文本文件并返回结果。在客户端,我在服务中使用以下函数来实际调用该端点

downloadZip(){

    const headers = new Headers({'Content-Type': 'application/json'});

    const token = localStorage.getItem('token')
        ? '?token=' + localStorage.getItem('token')
        : '';

    return this.http.get(this.endPoint + '/job/image-dl' + token, {headers: headers})
        .map((response: Response) => {
            const result = response;
            return result;
        })
        .catch((error: Response) => {
            this.errorService.handleError(error.json());
            return Observable.throw(error.json());
        });

}
Run Code Online (Sandbox Code Playgroud)

然后我有另一个函数,该函数调用downloadZip()该zip文件并将其实际下载到用户的本地浏览器中。

testfunc(){
    this.jobService.downloadZip().subscribe(
        (blah:any)=>{    
            var blob = new Blob([blah], {type: "application/zip"});
            FileSaver.saveAs(blob, "helloworld.zip");
        }
    );
}
Run Code Online (Sandbox Code Playgroud)

testfunc()被调用时,一个zip文件被下载到用户的浏览器中,但是当我尝试解压缩该文件时,它会创建一个zip.cpgz文件,当在无限循环中单击以表明发生某种损坏时,该文件会变成一个zip文件。谁能看到我在哪里出问题了?