我在使用exceljs的过程中创建了一个excel文件,并尝试在浏览器中下载相同的文件。但是损坏的文件正在下载。
此外,当我尝试使用POSTMAN击中端点时,文件已按预期下载。
我确实尝试查看https://github.com/exceljs/exceljs/issues/37,但是并没有太大帮助。
//Backend Code:
//My route file code, wherein workbook = new Excel.Workbook();
var fileName = 'FileName.xlsx';
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', 'attachment; filename=' + fileName);
return workbook.xlsx.write(res).then(function(){
res.end();
});
//Client Side Code:
$.ajax({
url: "/*****/download",
type: "GET",
data: body,
headers: {
'Content-type': 'application/json'
},
responseType: 'blob',
success: function (data) {
//Data contains the fields that I want to download
var saving = document.createElement('a');
var csvData;
//Using a FileSaver or Download library to download the file.
saveAs(new Blob([data], { type: "vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8" }), 'test.xlsx');
document.body.appendChild(saving);
saving.click();
document.body.removeChild(saving);
},
error: function (error) {
console.log(error);
}
});
在打开excel文件时,它说:我们发现某些内容有问题。...您要恢复...........
答案 0 :(得分:0)
您可以使用URL.createObjectURL
// after getting blob
const tempLink = URL.createObjectURL(blob)
const a = document.createElement('A')
a.download = 'output.xlsx'
a.href = tempLink
a.click()
URL.revokeObjectURL(tempLink) // release memory