我正在将zip文件从服务器发送到客户端,但是当文件已下载时,google会显示错误消息,表明该文件可能很危险,可能是因为该文件包含.js文件。有什么方法可以解决此问题。
问题:
function zipDirectory(source, out) {
const archive = archiver('zip', { zlib: { level: 9 }});
const stream = fs.createWriteStream(out);
return new Promise((resolve, reject) => {
archive
.directory(source, false)
.on('error', err => reject(err))
.pipe(stream)
;
stream.on('close', () => resolve(console.log('file compressed to zip')));
archive.finalize();
});
};
app.get('/site/:url/download', async(req,res) => {
console.log('get me');
await zipDirectory(path,zipPath);
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': `attachment; filename=${zipPath}`
});
const readStream = fs.createReadStream(zipPath);
readStream.on('open', function() {
readStream.pipe(res);
});
readStream.on('error', function(err) {
res.end(err);
});
});