/**
* 文件下载方法
* @param url
* @param data
*/
downloadFile(el,url, data) {
let requestData = Object.assign({}, data);
requestData= el.$Qs.stringify(requestData);
// 响应类型:arraybuffer, blob
el.$http.post(url, requestData, {responseType: 'blob'}).then(resp => {
let headers = resp.headers;
let contentType = headers['content-type'];
if (!resp.data) {
el.$message.error("暂无数据导出");
return false;
} else {
const blob = new Blob([resp.data], {type: contentType});
const contentDisposition = resp.headers['content-disposition'];
let fileName = 'unknown';
if (contentDisposition) {
fileName = window.decodeURI(resp.headers['content-disposition'].split('=')[1]);
}else{
el.$message.error("暂无数据导出");
return false;
}
BTools.bos.downFile(blob, fileName);
}
}).catch(function (error) {
console.log(error);
});
},
downFile(blob, fileName) {
// 非IE下载
if ('download' in document.createElement('a')) {
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob); // 创建下载的链接
link.download = fileName; // 下载后文件名
link.style.display = 'none';
document.body.appendChild(link);
link.click(); // 点击下载
window.URL.revokeObjectURL(link.href); // 释放掉blob对象
document.body.removeChild(link); // 下载完成移除元素
} else {
// IE10+下载
window.navigator.msSaveBlob(blob, fileName);
}
},
vue下载文件 前端
最新推荐文章于 2024-05-01 01:08:38 发布