后端返回二进制乱码,前端实现下载功能

接口部分

export function getfileName(fileName) {
  console.log(fileName)
  return request({
    url: '/file/'+fileName,
    method: 'get',
    headers: { 
      'Content-Type': 'pplication/octet-stream'
    }, 
    responseType: 'blob',
  })
}

vue文件部分

getfileName(row.filePath).then(res=>{
  if (res) {
      et temp = res.headers["content-disposition"]
     .split(";")[1]
     .split("=")[1];
      //对文件名乱码转义--【Node.js】使用iconv-lite解决中文乱码
     let iconv = require("iconv-lite");
     let fileName = iconv.decode(temp, "gbk");
     let type = decodeURIComponent(res.headers["content-type"]); // const xlsx = 'application/vnd.ms-excel'
     // console.log(type);
     const blob = new Blob([res.data], {
       type: "application/vnd.ms-excel;charset=UTF-8",
     });
     // console.log(blob);
     const blobUrl = window.URL.createObjectURL(blob);
     let a = document.createElement("a"); // 转换完成,创建一个a标签用于下载
      a .style.display = 'none'
     // 设置href属性为文件路径,download属性可以设置文件名称 
     a.href = blobUrl;
     a.download = fileName;
     document.body.appendChild(a);
     a.click();
     Window.URL.revokeObjectURL(blobUrl);
     a.remove();
   } else {
      this.$message.error("导出失败");
   } 
 })
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值