1. 程式人生 > >js 把字符串保存為txt文件,並下載到本地

js 把字符串保存為txt文件,並下載到本地

code element 下載 aud span html htm kit eat

代碼如下

          exportRaw(‘text.txt‘,‘123123123‘)

                function fakeClick(obj) {
                  var ev = document.createEvent("MouseEvents");
                  ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                  obj.dispatchEvent(ev);
                }

                
function exportRaw(name, data) { var urlObject = window.URL || window.webkitURL || window; var export_blob = new Blob([data]); var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a") save_link.href = urlObject.createObjectURL(export_blob); save_link.download
= name; fakeClick(save_link); }

js 把字符串保存為txt文件,並下載到本地