- 解决下载文件打不开 https://www.csdn.net/tags/MtTaMg4sNDcyMTgwLWJsb2cO0O0O.html
最主要是:导出文档,调用接口是设置 responseType: “blob” ,不能写在headers内,要单独设置。
Export(){
axios({
method: “get”,
url: xxxxxx,
responseType: “blob” , //必写
headers: {
‘token’: localStorage.getItem(‘token’)
}
}).then((res)=>{
//new blob对象,设置type为导出文件格式,这里以xls为列
let blob = new Blob([res.data],
{ type:‘application/vnd.ms-excel;charset=utf-8;’ }
);
const link = document.createElement("a"); //创建a标签
let fliaName = "xx.xls"
link.style.display = "none";
link.href = URL.createObjectURL(blob);
link.download = fliaName; //导出文件名
document.body.appendChild(link);
link.click();//执行下载
URL.revokeObjectURL(link.href); //释放url
document.body.removeChild(link); //释放标签
this.$message.success('导出成功!');
})
},
- 解决下载操作
https://blog.csdn.net/weixin_46381442/article/details/123779751
import axios from "axios"
import * as auth from '@/utils/auth.js'
let ajax = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 100000
});
ajax.interceptors.request.use(config => {
config.headers = {
Authorization: auth.getToken(),
// OrgId: auth.getUser().orgId,
// UserId: auth.getUser().id,
}
return config
},
err => {
return Promise.reject(err)
})
let downloadFile = async (url, formData, options) => {
await ajax.post(url, formData, {responseType: 'arraybuffer'}).then(resp => download(resp, options))
}
let getFile = async (url, options) => {
await ajax.get(url, {responseType: 'blob'}).then(resp => download(resp, options))
}
let download = (resp, options) => {
let blob = new Blob([resp.data], {type: options.fileType ? options.fileType : 'application/octet-binary'})
//创建下载的链接
let href = window.URL.createObjectURL(blob)
downloadBlob(href, options.fileName)
}
let downloadBlob = (blobUrl, fileName, revokeObjectURL) => {
let downloadElement = document.createElement('a')
downloadElement.href = blobUrl
//下载后文件名
downloadElement.download = fileName
document.body.appendChild(downloadElement)
//点击下载
downloadElement.click()
//下载完成移除元素
document.body.removeChild(downloadElement)
if (revokeObjectURL == null || revokeObjectURL) {
//释放掉blob对象
window.URL.revokeObjectURL(blobUrl)
}
}
let getDownloadFileUrl = async (url, fileType) => {
let blob
await ajax.get(url, {responseType: 'blob'}).then(resp => {
blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
})
return window.URL.createObjectURL(blob);
}
let getDownloadFileUrlByPost = async (url, data, fileType) => {
let blob
await ajax.post(url, data, {responseType: 'blob'}).then(resp => {
blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
})
return window.URL.createObjectURL(blob);
}
let getDownloadFileBlob = async (url, fileType) => {
let blob
await ajax.get(url, {responseType: 'blob'}).then(resp => {
blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
})
return blob;
}
export default {
ajax,
downloadFile,
getFile,
getDownloadFileUrl,
getDownloadFileUrlByPost,
getDownloadFileBlob,
downloadBlob
}
调用:
//先引用
import ajax from '../../utils/ajax.js'
//使用
ajax.downloadFile('URL',null,{下载的文件名称})