base64保存下載圖片


 1       exportPng() {
 2         let _this = this;
 3         let option = _this.charts.getOption();
 4     
 5         console.log(option);
 6         // 從echarts獲取圖片
 7         let imgData = _this.charts.getDataURL({
 8           type: "png",
 9           pixelRatio: 2,
10           backgroundColor: '#000',
11           excludeComponents: ['toolbox', 'dataZoom'],
12         });
13         _this.downloadFile(`${_this.chartsData.name}.png`, imgData);
14       },
15       // 下載
16       downloadFile(fileName, content) {
17         let aLink = document.createElement('a');
18         let blob = this.base64ToBlob(content); //new Blob([content]);
19     
20         let evt = document.createEvent("HTMLEvents");
21         evt.initEvent("click", true, true);//initEvent 不加后兩個參數在FF下會報錯  事件類型,是否冒泡,是否阻止瀏覽器的默認行為
22         aLink.download = fileName;
23         aLink.href = URL.createObjectURL(blob);
24     
25         // aLink.dispatchEvent(evt);
26         //aLink.click()
27         aLink.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true, view: window}));//兼容火狐
28       },
29       //base64轉blob
30       base64ToBlob(code) {
31         let parts = code.split(';base64,');
32         let contentType = parts[0].split(':')[1];
33         let raw = window.atob(parts[1]);
34         let rawLength = raw.length;
35     
36         let uInt8Array = new Uint8Array(rawLength);
37     
38         for (let i = 0; i < rawLength; ++i) {
39           uInt8Array[i] = raw.charCodeAt(i);
40         }
41         return new Blob([uInt8Array], {type: contentType});
42       },

摘抄CSDN博文,以此記錄供自己使用!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM