前端下载文件流并且获取heads中的filename文件名且解决中文乱码的方法

时间:2021-04-07 12:54:47   收藏:0   阅读:409

function axgetdata(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 => {
       //console.log(error);
});

原文:https://www.cnblogs.com/tangwei89/p/14626596.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!