js 下载 excel 文件乱码解决

方式一

原生js blob方式

downloadPlanData(planListIndex){
    
    
	let url_post = '/planFile';
	let params_post = {
    
    
	  planId: planListIndex,
	};
	// 利用a标签自定义下载文件名
	const link = document.createElement('a')
	axios.post(url_post, params_post, {
    
    responseType: 'arraybuffer'}).then(res => {
    
    
	  let filename = res.headers['content-disposition'].split('; ')[1].split('=')[1]
	  // 创建Blob对象,设置文件类型
	  let blob = new Blob([res.data], {
    
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'}); // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
	  var downloadElement = document.createElement('a');
	  var href = window.URL.createObjectURL(blob); // 创建下载的链接
	  downloadElement.href = href;
	  downloadElement.download = filename; // 下载后文件名
	  document.body.appendChild(downloadElement);
	  downloadElement.click(); // 点击下载
	  document.body.removeChild(downloadElement); // 下载完成移除元素
	  window.URL.revokeObjectURL(href); // 释放掉blob对象
	})
}

猜你喜欢

转载自blog.csdn.net/weixin_41822224/article/details/107639615