JavaScript将字符串转换为文档下载
downloadFile.js
/**
* @param {string} fileName
* @param {string} content
*/
export function downloadFile(fileName, content) {
const a = document.createElement('a');
const event = document.createEvent('MouseEvents');
const blob = new Blob([content]);
a.href = URL.createObjectURL(blob);
a.download = fileName;
event.initEvent('click', true, true);
a.dispatchEvent(event);
}
在需要下载的地方引入js文件
import { downloadFile } from '@/utils/downloadFile';
使用downloadFile方法进行文件下载
async handleDownloadFile() {
downloadFile(this.configName, this.detailData);
},