前端通过blob方式实现导出表格

1.需要后台的配合,后台返回文件流
在这里插入图片描述
2.

exportReport(url,params){
	axios({
	method:get,
	url: url,
	params: params,
	responseType: ‘blob’
}).then((res) => {
	const link = document.createElement('a')
 	let blob = new Blob([res.data],{type: 'application/vnd.ms-excel'});
 	//获取heads中的filename文件名
 	let temp = res.headers["content-disposition"].split(";")[1].split("filename=")[1];
 	//对文件名乱码转义--【Node.js】使用iconv-lite解决中文乱码
 	let iconv = require('iconv-lite'); 
     iconv.skipDecodeWarning = true;//忽略警告
 	let fileName = iconv.decode(temp, 'gbk');
 	console.log('fileName_',fileName)
 	// return
 	link.style.display = 'none'
 	link.href = URL.createObjectURL(blob);
 	link.setAttribute('download', fileName)
 	document.body.appendChild(link)
 	link.click()
 	document.body.removeChild(link)
}).catch(error => {})
}

你可能感兴趣的:(vue)