1.导出接口安装 js-file-download
npm install js-file-download
2、引入
import fileDownload from ‘js-file-download’;
3.使用.
import Axios from 'axios'
import store from '@/store' // 获取token
deond() {
var requestUrl = "tcmp/api/devices/model";
var Authorization = store.state.user.token;
Axios({
method: 'get',
url: requestUrl,
headers: {
'Authorization': Authorization
},
responseType: 'blob'
}).then(res => {
fileDownload(res.data, 'deviceModel.xlsx');
});
}
4.不走接口下载
// let url ='图片地址'
// //将url地址转换为blob地址
// //跳过浏览直接下载
// fetch(url)
// .then(res => res.blob())
// .then(blob => {
// let url = window.URL.createObjectURL(blob);
// let a = document.createElement("a");
// a.style.display = "none";
// a.href = url;
// a.download = "filename";
// document.body.appendChild(a);
// a.click();
// document.body.removeChild(a);
// window.URL.revokeObjectURL(url);
// });