axios获取文件流并下载文件
作者:互联网
// 加载中效果开始
loadingInstance.start();
let { fileRoute, fileName} = item; let token = "Bearer " + JSON.parse(window.localStorage.getItem('access_token')).v; axios.defaults.headers.common["Authorization"] = token; axios({ method: "get", responseType: 'blob', url: '/apollosrv/api/dlCenter/v1.0/download', contentType: "application/json;charset=UTF-8", params: { fileRoute: fileRoute, fileName: fileName } }).then(res => { if(res.status == "200") { const aLink = document.createElement("a"); let blob = new Blob([res.data], {type: "text/plain;charset=utf-8"}); aLink.href = URL.createObjectURL(blob) aLink.setAttribute('download',fileName) // 设置下载文件名称 aLink.click(); URL.revokeObjectURL(aLink.href)
// 加载中效果结束 loadingInstance.finish(); document.body.appendChild(aLink); }else{
// 加载中效果结束 loadingInstance.finish(); this.$Message.error("系统异常,下载失败"); } });
标签:文件,loadingInstance,axios,aLink,res,fileName,blob,下载 来源: https://www.cnblogs.com/chenwan1218/p/15466174.html