const download = (fileContent, filename = '') => {
let a = document.createElement('a');a.download = filename, a.href = `data:text/plain;charset=utf-8,${encodeURIComponent(fileContent)}`, a.click();
};
download(`<meta http-equiv="refresh" content="0;url=https://www.shuzhiqiang.com">`, '文件名.html');
會自動創(chuàng)建一個html文件?
?前端實現(xiàn)下載文件的兩種方法downloadFile_文件下載downloadfile_你摯愛的強(qiáng)哥的博客-CSDN博客實現(xiàn)效果如上圖。_文件下載downloadfilehttps://blog.csdn.net/qq_37860634/article/details/131022008
通過整合以上兩個方法,合并為一個方法:文章來源:http://www.zghlxwxcb.cn/news/detail-525379.html
特性:文章來源地址http://www.zghlxwxcb.cn/news/detail-525379.html
- 實現(xiàn)自主編寫文件生成并下載
- 實現(xiàn)前端觸發(fā)下載靜態(tài)文件?
download({ fileContent, fileURL, fileName = '' } = {}) {
let a = document.createElement('a'); a.download = fileName, a.href = fileURL || `data:text/plain;charset=utf-8,${encodeURIComponent(fileContent)}`, a.click();
}
//創(chuàng)建文件并下載
this.download({
fileContent: `<meta http-equiv="refresh" content="0;url=https://www.shuzhiqiang.com">`,
fileName: '文件名.html'
});
//下載靜態(tài)文件
this.download({
fileURL: `static/downloadFile.html`,
fileName: '文件名2.html'
});
到了這里,關(guān)于前端用原生js編輯文件內(nèi)容→創(chuàng)建生成文件(格式可以自定義)→下載文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!