js下载blob文件


header设置
if (responseType == 'blob') {
headerJosn['content-disposition'] = "attachment;filename=total.xls"
headerJosn['content-type'] = 'application/x-download;charset=utf-8';
}
if (responseType == 'upload') {
headerJosn['content-type'] = 'multipart/form-data';
}
this.$httpRequest.get(url, data, 'blob').then(res => {
try {
//这里res是返回的blob对象
  let blob = new Blob([res], {
   type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
  }); //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
  // 下载类型大全 https://blog.csdn.net/yin_you_yu/article/details/116261304
  let downloadElement = document.createElement('a');
  let href = window.URL.createObjectURL(blob); //创建下载的链接
  downloadElement.href = href;
  let time = (new Date()).valueOf();
  downloadElement.download = '下载名称' + time + '.csv'; //下载后文件名
  document.body.appendChild(downloadElement);
  downloadElement.click(); //点击下载
  document.body.removeChild(downloadElement); //下载完成移除元素
  window.URL.revokeObjectURL(href); //释放掉blob对象
} catch (e) {}
})


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM