// 附件预览下载
downloadFile: params => {
return new Promise((resolve, reject) => {
axios({
method: "get",
url: `${process.env.VUE_APP_BASE_API}/common/downloadFile`, // 请求地址
params, // 参数
responseType: "blob", // 表明返回服务器返回的数据类型
headers: {
Authorization: sessionStorage.getItem('loginToken') || ''
}
}).then(value => {
if (params.openOnLine) {
let binaryData = [];
binaryData.push(value.data);
const previewSrc = window[window.webkitURL ? 'webkitURL' : 'URL'].createObjectURL(new Blob(binaryData));
resolve(previewSrc)
} else {
let MIME = {
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
png: 'image/png',
webp: 'image/webp',
xls: 'application/vnd.ms-excel',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
doc: 'application/msword',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
ppt: 'application/vnd.ms-powerpoint',
pdf: 'application/pdf'
};
let blob = new Blob([value.data], {
type: MIME[params.fileType]
});
let fileName = params.fileName;
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
// 释放内存
window.URL.revokeObjectURL(link.href);
}
}
}).catch(err => {
reject(err);
});
})
},
网友评论