<html> <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" 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> </html>
点击保存按钮将下载到本地