通常“res.status(200).send(data)”我返回发送数据
我使用了 adm-zip 和 achiver。
节点
const loadReasearchReport = async (req, res) => {
try {
const resultId = req.query.id;
const reportType = req.query.type;
const result = await resultHeaderRepo.findById(req.query.id);
if (!result) {
console.log(`Not Registered On Result DataBase :${reportType} ${resultId}`)
}
const filePath = result.path;
const reportData = await reportGenerator.generateMicrobiomeReportData(filePath, TEMP_FOLDER, reportType);
let create = await microbiomeReprter.createReportPDFKit(reportData, reportType);
let reportName = reportType.replaceAll(' ', '_');
const fileName = reportName + '_' + result.resultNo + '.zip';
//(admZip)
let admZip = require('adm-zip');
let zip = new admZip();
// add local file
zip.addLocalFolder(reportData.workSpace);
let zipFileContents = zip.toBuffer();
const fileType = 'application/zip';
res.writeHead(200, {
'Content-Disposition': `attachment; filename="${fileName}"`,
'Content-Type': fileType,
})
return res.end(zipFileContents)
// return res.status(200).send(reportType);
} catch (error) {
console.log(error)
}
}
Vue.js
let params = [];
params = {
"id": this.resultId,
"type": this.reportType,
};
let request = {params : params};
await this.$axios.get(this.$apiHost + "/results/research", request,{
responseType: 'blob',
headers:{
'Content-Type': 'application/json; application/octet-stream'
},
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]),{
type:'application/zip'
});
console.log(url)
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.zip'); //or any other extension
document.body.appendChild(link);
link.click();
link.remove();
return false;
});
“ReportData.workSpace”是本地存储的文件夹。
zip文件将被下载,但它说它在打开时已损坏
我被这个问题困住了
你能帮助我吗?