纯前端保存下载文件到本地

保存文件为txt,浏览器下载到本地。使用Blob对象

const data = "内容" // 这里填内容的字符串
const blob = new Blob([data], { type: "text/plain" })
const a = document.createElement("a")
a.href = URL.createObjectURL(blob)
a.download = "fileName.txt" // 这里填保存成的文件名
a.click()
URL.revokeObjectURL(a.href)
a.remove();

 

posted @ 2021-08-20 15:44  herry菌  阅读(562)  评论(0编辑  收藏  举报