我正在尝试在服务器上压缩多个文件并在用户请求时下载它。我正在使用 adm-zip 压缩文件。
文件添加得非常好并且被压缩。zip.writeZip('') 完美地压缩文件并将它们保存到本地服务器。我只是无法下载它们。无论哪种方式,我认为如果我可以直接通过缓冲区发送 zip 会更好。
router.get(
"/download-zip/:season_id/:club_id",
passport.authenticate("jwt", { session: false }),
(req, res) => {
FileSubmission.find({
season: req.params.season_id,
club: req.params.club_id
}).then(files => {
const zip = new AdmZip();
files.forEach(file => {
// add local file
zip.addLocalFile(`uploads/club-uploads/${file.club}/${file.name}`);
});
res.download(zip.toBuffer(), "asd.zip");
});
}
);
在前面,我正在使用对动作和 js-file-download 库做出反应
// Download ALL FILES by season ID and club ID
import fileDownload from "js-file-download";
export const downloadAllFiles = (season_id, club_id) => dispatch => {
axios
.get(`/api/files/download-zip/${season_id}/${club_id}`)
.then(response => {
fileDownload(response.data, `All Files- ${club_id}.zip`);
})
.catch(err => {
console.log(err);
dispatch({ type: GET_ERRORS, payload: err.response.data });
});
};