1 一、安裝 2 # Basic Node.JS installation 3 npm install file-saver --save 4 bower install file-saver 5 此外,可以通過以下方式安裝TypeScript定義: 6 7 # Additional typescript definitions 8 npm install @types/file-saver --save-dev 9 二、語法 10 saveAs()從文件保存器導入 11 import { saveAs } from 'file-saver'; 12 FileSaver saveAs(Blob/File/Url, optional DOMString filename, optional Object { autoBom }) 13 傳遞{ autoBom: true }如果你想FileSaver.js自動提供Unicode文本編碼提示(:見字節順序標記)。請注意,只有在您的Blob類型已charset=utf-8設置的情況下才能執行此操作。 14 15 16 17 三、例子 18 使用保存文字 require() 19 var FileSaver = require('file-saver'); 20 var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"}); 21 FileSaver.saveAs(blob, "hello world.txt"); 22 儲存文字 23 var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"}); 24 FileSaver.saveAs(blob, "hello world.txt"); 25 保存網址 26 FileSaver.saveAs("https://httpbin.org/image", "image.jpg"); 27 在相同來源內使用URL只會使用a[download]。否則,它將首先檢查它是否支持帶有同步頭請求的cors標頭。如果是這樣,它將下載數據並使用Blob URL保存。如果沒有,它將嘗試使用下載它a[download]。 28 29 標准的W3C File API Blob接口並非在所有瀏覽器中都可用。 Blob.js是Blob解決此問題的跨瀏覽器實現。 30 31 保存畫布 32 var canvas = document.getElementById("my-canvas"); 33 canvas.toBlob(function(blob) { 34 saveAs(blob, "pretty image.png"); 35 }); 36 注意:標准HTML5 canvas.toBlob()方法並非在所有瀏覽器中都可用。 canvas-toBlob.js是一個跨瀏覽器canvas.toBlob(),可以對此進行填充。 37 38 保存文件 39 您可以保存File構造函數而無需指定文件名。如果文件本身已經包含名稱,則有很多方法可以獲取文件實例(從存儲,文件輸入,新構造函數,剪貼板事件)。如果仍要更改名稱,則可以在第二個參數中更改它。 40 41 // Note: Ie and Edge don't support the new File constructor, 42 // so it's better to construct blobs and use saveAs(blob, filename) 43 var file = new File(["Hello, world!"], "hello world.txt", {type: "text/plain;charset=utf-8"}); 44 FileSaver.saveAs(file); 45 46 四、具體使用 47 下面是項目中使用file-saver封裝幾種常見格式的導出,這里后台主要輸出文件流形式,如下: 48 49 50 51 在文件exportFile.js中封裝方法: 52 53 import FileSaver from "file-saver"; 54 export default class fileSave { 55 /** 56 * 導出Excel文件 57 * @param {*} res 文件流 58 * @param {*} name 文件名 59 */ 60 static getExcel(res, name) { 61 let blob = new Blob([res], { 62 type: "application/vnd.ms-excel" 63 }); 64 FileSaver.saveAs(blob, name + ".xlsx"); 65 } 66 67 /** 68 * 導出CSV文件 69 * @param {*} res 文件流 70 * @param {*} name 文件名 71 */ 72 static getCsv(res, name) { 73 let blob = new Blob([res], { 74 type: "application/vnd.ms-excel" 75 }); 76 FileSaver.saveAs(blob, name + ".csv"); 77 } 78 79 /** 80 * 導出圖片1 81 * @param {*} url 圖片地址 82 * @param {*} name 文件名 83 */ 84 static getImgURLs(url, name) { 85 let last = url.substring(url.lastIndexOf("."), url.length); 86 FileSaver.saveAs(url, `${name}${last}`); 87 } 88 /** 89 * 導出圖片2 90 * @param {*} res 文件流 91 * @param {*} name 文件名 92 */ 93 static downLoadImg(res, filename) { 94 let blob = new Blob([res], { 95 type: "image/jpeg" 96 }); 97 FileSaver.saveAs(blob, `${filename}.jpg`); 98 } 99 } 100 使用: 101 102 1.導入 103 104 import exportFile from '@/utils/exportFile' 105 2.使用 106 107 exportFile.getExcel(res.data, '近年走勢') 108 109 ———————————————— 110 版權聲明:本文為CSDN博主「Drss」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。 111 原文鏈接:https://blog.csdn.net/qq_30671099/java/article/details/104052782