1.方法封装,data:文件流, fileName文件名,接口请求需要加responseType: “blob”
export function downloadFile(data, fileName) {
const blob = new Blob([data]);
const downloadElement = document.createElement(“a”);
// 创建下载的链接
const href = window.URL.createObjectURL(blob);
downloadElement.href = href;
// 下载后文件名
downloadElement.download = fileName;
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
// 释放掉blob对象
window.URL.revokeObjectURL(href);
}