数据输出

to saving files on the client-side, and is perfect for web apps that generates files on the client, However if the file is coming from the server we recommend you to first try to…

FileSave

依赖

1
npm install file-saver --save

使用

1
2
3
4
5
import saveAs from 'file-saver';

let content = JSON.stringify(this.voiceData)
let blob = new Blob([content], { type: "bin/plain;charset=utf-8" })
saveAs(blob, 'voiceData.json')

纯JS页面数据导出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 获取数据
var getData = new Date()

// 下载文件方法
var funDownload = function (content, filename) {
var eleLink = document.createElement('a');
eleLink.download = filename;
eleLink.style.display = 'none';
// 字符内容转变成blob地址
var blob = new Blob([content]);
eleLink.href = URL.createObjectURL(blob);
// 触发点击
document.body.appendChild(eleLink);
eleLink.click();
// 然后移除
document.body.removeChild(eleLink);
};

if ('download' in document.createElement('a')) {
// 作为test.html文件下载
document.getElementById('download').addEventListener('click', function () {
console.log(getData);
funDownload(getData, 'test.json');
});
} else {
eleButton.onclick = function () {
alert('浏览器不支持');
};
}