// // 从 response 的 headers 中获取 filename, 后端 response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
var patt = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
var contentDisposition = decodeURI(res.headers["content-disposition"]);
var result = patt.exec(contentDisposition);
var fileName = result[1];
fileName = fileName.replace(/\"/g,"");
// IE 兼容办法
if (window.navigator && window.navigator.msSaveOrOpenBlob) {window.navigator.msSaveOrOpenBlob(res.data, fileName);
return;
}
const aLink = document.createElement("a");
var blob = new Blob([res.data], {type: mimeType});
aLink.href = URL.createObjectURL(blob);
aLink.setAttribute("download", fileName); // 设置下载文件名称
document.body.appendChild(aLink);
aLink.click();