首页 技术 正文
技术 2022年11月17日
0 收藏 890 点赞 4,098 浏览 876 个字
一直以来,JS都没有比较好的可以直接处理二进制的方法。而Blob的存在,允许我们可以通过JS直接操作二进制数据。
一、下载
util.fetchDownload= function (opt,data) {
return fetch(opt.url,{
method: "POST",
headers: {
'Content-Type': 'application/json',
},
mode: "cors",
body: JSON.stringify(data),
credentials: 'include'
}).then(resp => resp.blob().then(blob => {
const disposition = resp.headers.get("content-disposition");
const match = disposition ? disposition.match(/attachment; filename="(.+)"/i) : null;
const filename = match && match.length > 1 ? match[1] : snTime + ".xls";
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename); //兼容ie10
} else {
var a = document.createElement('a');
document.body.appendChild(a) //兼容火狐,将a标签添加到body当中
var url = window.URL.createObjectURL(blob); // 获取 blob 本地文件连接 (blob 为纯二进制对象,不能够直接保存到磁盘上)
a.href = url;
a.download = filename;
a.target='_blank' // a标签增加target属性
a.click();
a.remove() //移除a标签
window.URL.revokeObjectURL(url);
}
}).then(function () {
opt.success()
}))};
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902