使用 new Blob() 将文本框数据保存为 txt文件
作者:互联网
<textarea name="" id="text" cols="40" rows="10">
第一行:这里输入的数据将保存为txt中
第二行:保存为文件
</textarea>
<button id="save" type="button">保存</button>
<script>
document.querySelector("#save").addEventListener("click", saveFile);
function saveFile() {
var inValue = document.querySelector("#text").value;
var blob = new Blob([inValue], { type: "text/plain;charset=utf-8" });
const fileName = "test.txt";
console.log("msSaveOorOpenBlob" in navigator)
if ("msSaveOorOpenBlob" in navigator) {
//IE 浏览器
window.navigator.msSaveOorOpenBlob(blob, fileName);
} else {
//不是IE浏览器
var url = window.URL.createObjectURL(blob);
var link = document.createElement("a");
link.href = url;
link.setAttribute("download", fileName);
link.click();
}
}
</script>
标签:文本框,fileName,link,Blob,var,new,navigator,txt,blob 来源: https://www.cnblogs.com/dailyStudy/p/16134436.html