其他分享
首页 > 其他分享> > axios下载文件

axios下载文件

作者:互联网

download () {
	let url ="" ;
	const link = document.createElement("a"); //创建个a标签
    link.href = url;
    // link.download = fileName;//重命名
    link.click();
    URL.revokeObjectURL(url); //释放内存
},

主要就是设置responseType: 'blob',缺点是ajax下载的话你在浏览器看不到进度显示

testdow(){
	var conf = {responseType: 'blob'}
	$http.get("",conf).then(data => {	
			this.downloadByBlob(data, "xls", "test");
		 }).catch(error => {
	});
},
downloadByBlob(data, fileType, fileName) { 
	try {
		let url = window.URL.createObjectURL(data);
		let eleLink = document.createElement('a');
		eleLink.href = url;
		eleLink.download = `${fileName}.${fileType}`;
		document.body.appendChild(eleLink);
		eleLink.click();
		window.URL.revokeObjectURL(url);
	} catch (error) {
		console.error('download function error!', error);
	}
}

标签:文件,axios,url,link,error,download,eleLink,data,下载
来源: https://blog.csdn.net/FIQ_527/article/details/122448596