vue 文件流下载pdf文件
2021-03-23 本文已影响0人
zhudying
/***
* @author zhudying
* @description 文件流下载pdf
* @function exportPdf pdf下载函数
* @param data 文件流数据,后端请求返回
* @param {string} fileName 文件名(xx.pdf)
*
* ***/
export function exportPdf(data, fileName) {
let blob = new Blob([data], {
//type类型后端返回来的数据中会有,根据自己实际进行修改
// 表格下载为 application/xlsx,压缩包为 application/zip等,
type: "application/pdf"
});
let filename = fileName;
if (typeof window.navigator.msSaveBlob !== "undefined") {
window.navigator.msSaveBlob(blob, filename);
} else {
var blobURL = window.URL.createObjectURL(blob);
// 创建隐藏<a>标签进行下载
var tempLink = document.createElement("a");
tempLink.style.display = "none";
tempLink.href = blobURL;
tempLink.setAttribute("download", filename);
if (typeof tempLink.download === "undefined") {
tempLink.setAttribute("target", "_blank");
}
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
}
注:
- 在axios请求时,需要添加responseType: 'arraybuffer'
2.new Blob的type类型详情点击查看