Saving text using require()
var FileSaver = require('file-saver');
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
Saving URLs
FileSaver.saveAs("https://httpbin.org/image", "image.jpg");
Saving a canvas
var canvas = document.getElementById("my-canvas");
canvas.toBlob(function(blob) {
saveAs(blob, "pretty image.png");
});
/**
* 导出json
* @param {Object | Array} json // 导出的json
* @param {String} fileName // 文件名
*/
export const saveDataAsJson = function(json, fileName) {
const blob = new Blob([JSON.stringify(json, null, 2)], { type: 'text/plain;charset = utf-8' })
FileSaver.saveAs(blob, fileName, { autoBom: false })
}
saveDataAsCsv({ name: '章三', age: 10 }, 'data.json')
npm Url
https://www.npmjs.com/package/file-saver