首先说一下主要思路:
- 将数据转为二进制
- 创建a链接,将数据的二进制通过 urlObject.createObjectURL(exportBlob);转为a的href
- 点击a链接
save () {
// 获取数据
let text = ''
this.fileData.forEach(val => {
text += val.content + '\n'
})
// 命名
let name = this.imgData.doc_data.title
this.exportRaw(name, text);
},
exportRaw (name, data) {
var urlObject = window.URL || window.webkitURL || window;
var exportBlob = new Blob([data]);
var saveLink = document.createElement('a')
saveLink.href = urlObject.createObjectURL(exportBlob);
saveLink.download = name;
this.fakeClick(saveLink);
},
fakeClick(obj) {
var ev = document.createEvent('MouseEvents');
ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
obj.dispatchEvent(ev);
}