jjddrushi 2019-12-27
不是不兼容ie,就是不兼容ff,费了很多时间感谢原作者.
背景
在项目开发过程中,经常会有图片导出的需求,尤其是带有图表类的应用,通常需要将图表下载导出。
在chrome等新版浏览器中实现base64图片的下载还是比较容易的:
但是这套逻辑在IE下是不行的,这样写会直接报错。
所以IE下需要单独处理,这里IE在处理这种文件的时候给提供了一个单独的方法:window.navigator.msSaveOrOpenBlob(blob, download_filename)调用这个方法可以直接触发IE的下载,还是比较方便的。具体做法如下:
// 截取base64的数据内容(去掉前面的描述信息,类似这样的一段:data:image/png;base64,)并解码为2进制数据 var bstr = atob(imgUrl.split(‘,‘)[1]) // 获取解码后的二进制数据的长度,用于后面创建二进制数据容器 var n = bstr.length // 创建一个Uint8Array类型的数组以存放二进制数据 var u8arr = new Uint8Array(n) // 将二进制数据存入Uint8Array类型的数组中 while (n--) { u8arr[n] = bstr.charCodeAt(n) } // 创建blob对象 var blob = new Blob([u8arr]) // 调用浏览器的方法,调起IE的下载流程 window.navigator.msSaveOrOpenBlob(blob, ‘chart-download‘ + ‘.‘ + ‘png‘)
整体代码// 这里是获取到的图片base64编码,这里只是个例子哈,要自行编码图片替换这里才能测试看到效果const imgUrl = ‘data:image/png;base64,...‘
// 如果浏览器支持msSaveOrOpenBlob方法(也就是使用IE浏览器的时候),那么调用该方法去下载图片 if (window.navigator.msSaveOrOpenBlob) { var bstr = atob(imgUrl.split(‘,‘)[1]) var n = bstr.length var u8arr = new Uint8Array(n) while (n--) { u8arr[n] = bstr.charCodeAt(n) } var blob = new Blob([u8arr]) window.navigator.msSaveOrOpenBlob(blob, ‘chart-download‘ + ‘.‘ + ‘png‘) } else { // 这里就按照chrome等新版浏览器来处理 const a = document.createElement(‘a‘) a.href = URL.createObjectURL(blob);//imgUrl a.setAttribute(‘download‘, ‘chart-download‘) //a.click() a.dispatchEvent(new MouseEvent(‘click‘, {bubbles: true, cancelable: true, view: window}));//兼容火狐 }
不是不兼容ie,就是不兼容ff,费了很多时间感谢原作者.
<img name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aa7XRfiR"; document.all[‘pic1‘].src = "data:image/jpg;base64," + ret; //下载图片 function download(){ let imgData = "data:image/jpg;base64," + ret; this.downloadFile(‘测试.png‘, imgData); } //下载 function downloadFile(fileName, content) { let aLink = document.createElement(‘a‘); let blob = this.base64ToBlob(content); //new Blob([content]); let evt = document.createEvent("HTMLEvents"); evt.initEvent("click", true, true);//initEvent 不加后两个参数在FF下会报错 事件类型,是否冒泡,是否阻止浏览器的默认行为 aLink.download = fileName; aLink.href = URL.createObjectURL(blob); // aLink.dispatchEvent(evt); aLink.click() } //base64转blob function base64ToBlob(code) { let parts = code.split(‘;base64,‘); let contentType = parts[0].split(‘:‘)[1]; let raw = window.atob(parts[1]); let rawLength = raw.length; let uInt8Array = new Uint8Array(rawLength); for (let i = 0; i < rawLength; ++i) { uInt8Array[i] = raw.charCodeAt(i); } return new Blob([uInt8Array], {type: contentType}); } </script>
参考引用
https://www.jb51.net/article/147431.htm