JS 前端下载各种不同类型的文件

NO.1

摘自文章: https://blog.csdn.net/weixin_43833570/article/details/97010455
前端处理数据,导出excel
var uInt8Array = new Uint8Array(content); //先将返回的二进制数组转化为js的二进制数组
let blob=new Blob([uInt8Array],{type:"application/vnd.ms-excel"});//然后创建blob对象,文件类型设置为excel的类型
let blobURL = window.URL.createObjectURL(blob);//然后创建一个可访问的URL
let tempLink = document.createElement('a');//创建a标签去下载
tempLink.style.display = 'none';
tempLink.href = blobURL;
tempLink.setAttribute('download', fileName+".xlsx");
if (typeof tempLink.download === 'undefined') {
    tempLink.setAttribute('target', '_blank');
}
 document.body.appendChild(tempLink);
 tempLink.click();
 document.body.removeChild(tempLink);
 window.URL.revokeObjectURL(blobURL);

NO.2

摘自文章: https://code-examples.net/zh-TW/q/182e049
var downloadBlob, downloadURL;

downloadBlob = function(data, fileName, mimeType) {
  var blob, url;
  blob = new Blob([data], {
    type: mimeType
  });
  url = window.URL.createObjectURL(blob);
  downloadURL(url, fileName, mimeType);
  setTimeout(function() {
    return window.URL.revokeObjectURL(url);
  }, 1000);
};

downloadURL = function(data, fileName) {
  var a;
  a = document.createElement('a');
  a.href = data;
  a.download = fileName;
  document.body.appendChild(a);
  a.style = 'display: none';
  a.click();
  a.remove();
};

downloadBlob(myBinaryBlob, 'some-file.bin', 'application/octet-stream');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值