1、条件是vue项目安装了axios,如果没有安装可以安装一下
npm install axios
2、在vue页面引入 axios;
import axios from 'axios'
3、下载方法如下
async file_download(row){
var u="你的ip+端口"+"/system/item/downloadZip?itemId="+row.id;
// axios.defaults.withCredentials=false;
//axios.defaults.crossDomain=false;
axios.defaults.headers.common['Authorization'] = "你的token";
axios({
method:'get',
url:u,
responseType:'blob',
})
.then(function(response) {
var blob = new Blob([response.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'}); //application/vnd.openxmlformats-officedocument.wordprocessingml.document这里表示doc类型
var contentDisposition = response.headers['content-disposition']; //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
var patt = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
var result = patt.exec(contentDisposition);
var filename = result[1];
var downloadElement = document.createElement('a');
var href = window.URL.createObjectURL(blob); //创建下载的链接
var reg = /^["](.*)["]$/g;
downloadElement.style.display = 'none';
downloadElement.href = href;
downloadElement.download = decodeURI(filename.replace(reg,"$1")); //下载后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
document.body.removeChild(downloadElement); //下载完成移除元素
window.URL.revokeObjectURL(href); //释放掉blob对象
});
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)