这两天写一个后台生成Excel返回前端下载的功能,遇到了一个问题,记录一下。
前端点击下载按钮,文档损坏,但是使用Postman调用下载,文档却是正常的。
exportExcel(exportExcelParams.value).then((res: any) => {
let blob = new Blob([res], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8',
});
let link = document.createElement('a');
link.style.display = 'none';
link.href = URL.createObjectURL(blob);
link.download = exportExcelParams.value.expName; //下载的文件名
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
在请求方法中加入 responseType: ‘blob’ 后,问题解决,如下所示:
export const exportExcel = async (SysExportSetReq: any) => {
return request.request({
url: '/sam/master/baSysExportSet/exportExcel',
method: 'POST',
data: SysExportSetReq,
responseType: 'blob'
});
};
Original: https://www.cnblogs.com/JyoKou/p/16545939.html
Author: JyoKou
Title: JS使用BLOB方式下载Excel导致文件损坏的问题解决
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/590165/
转载文章受原作者版权保护。转载请注明原作者出处!