vue 下载文件
2021-09-26 本文已影响0人
小米和豆豆
接口调用成功之后核心逻辑 注意接口请求时 设置responseType: 'blob'
var blob = new Blob([res.data], { 'content-type': 'application/octet-stream;charset=utf-8'});
var contentDisposition = res.headers["content-disposition"];
console.log(contentDisposition)
let newfileName = '';
if (contentDisposition) {
newfileName = decodeURI(contentDisposition.split('=')[1]);
fileName = newfileName;
}
var a = document.createElement('a');
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
var body = document.getElementsByTagName('body')[0];
body.appendChild(a);
a.click();
body.removeChild(a);
window.URL.revokeObjectURL(url);