1.运用FileSave.js
导出text
npm install file-saver --save /*装置*/ import FileSaver from 'file-saver' /*导入*/ var Blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"}) /*要导出的数据格式*/ FileSaver.saveAs(blob, 'HelloWord.txt')
导出file
/*同导出text*/ var file = new File(["Hello, world!"], "hello world.txt", {type: "text/plain;charset=utf-8"}) FileSaver.saveAs(file)
2.文件较多时,紧缩导出(运用jszip)
npm install jszip --save import JSZip from 'jszip' const Zip = new JSZip() data.map(data => { const blob = new Blob(data, {type: "text/plain;charset=utf-8"}) Zip.file(data.name, blob) }) /*根据具体情况处置惩罚每一个文件的数据*/ Zip.generateAsync({type:"blob"}).then(function(files) { saveAs(files, "一个紧缩包.zip") })