js 實現下載資料到檔案裡面
阿新 • • 發佈:2021-06-28
直接上程式碼 function doSave(value, type, name) { var blob; if (typeof window.Blob == "function") { blob = new Blob([value], {type: type}); } else { var BlobBuilder = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder; var bb = newBlobBuilder(); bb.append(value); blob = bb.getBlob(type); } var URL = window.URL || window.webkitURL; var bloburl = URL.createObjectURL(blob); var anchor = document.createElement("a"); if ('download' in anchor) { anchor.style.visibility = "hidden"; anchor.href= bloburl; anchor.download = name; document.body.appendChild(anchor); var evt = document.createEvent("MouseEvents"); evt.initEvent("click", true, true); anchor.dispatchEvent(evt); document.body.removeChild(anchor); } else if (navigator.msSaveBlob) { navigator.msSaveBlob(blob, name); }else { location.href = bloburl; } } var a = 1; function Save(){ doSave(a, "text/latex", "hello.txt"); } Save()