我是Node的初学者,我正在尝试弄清楚如何在服务器上创建一个zip文件,然后将其发送到客户端,然后将zip文件下载到用户的浏览器 . 我正在使用Express框架,我正在使用Archiver来实际进行压缩 . 我的服务器代码如下,取自Dynamically create and stream zip to client

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

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

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

}

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

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

调用 testfunc() 时,会将zip文件下载到用户的浏览器中,但是当我尝试解压缩它时会创建一个zip.cpgz文件,然后在无限循环中单击时会转回zip文件,表明发生了某种损坏 . 有谁能看到我在哪里出错?