我是Node的初学者,我正在尝试弄清楚如何在服务器上创建一个zip文件,然后将其发送到客户端,然后将zip文件下载到用户的浏览器中。我正在使用Express框架,我正在使用Archiver来实际进行压缩。我的服务器代码来自Dynamically create and stream zip to client
<input type="file" accept="image/*" capture="camera">
所以它压缩两个文本文件并返回结果。在客户端,我在服务中使用以下函数来实际调用该端点
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();
});
然后我有另一个函数调用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());
});
}
并实际将zip文件下载到用户的本地浏览器。
downloadZip()
当调用testfunc(){
this.jobService.downloadZip().subscribe(
(blah:any)=>{
var blob = new Blob([blah], {type: "application/zip"});
FileSaver.saveAs(blob, "helloworld.zip");
}
);
}
时,会将zip文件下载到用户的浏览器中,但是当我尝试解压缩时会创建一个zip.cpgz文件,然后在单击时将其转换为zip文件一个无限循环,表明发生了某种腐败。谁能看到我在哪里错了?