所以就想說將網頁上的純文字內容輸出為一個 txt 檔案
saveTextAsFile function 內有兩個參數
第一個參數 _fileName 為預設在本地端存檔的檔名
第二個參數 _text 為輸出的純文字內容
function saveTextAsFile( _fileName, _text ) { var textFileAsBlob = new Blob([_text], {type:'text/plain'}); var downloadLink = document.createElement("a"); downloadLink.download = _fileName; downloadLink.innerHTML = "Download File"; if (window.webkitURL != null) { // Chrome allows the link to be clicked // without actually adding it to the DOM. downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob); } else { // Firefox requires the link to be added to the DOM // before it can be clicked. downloadLink.href = window.URL.createObjectURL(textFileAsBlob); downloadLink.onclick = destroyClickedElement; downloadLink.style.display = "none"; document.body.appendChild(downloadLink); } downloadLink.click(); } function destroyClickedElement(event) { document.body.removeChild(event.target); }
沒有留言:
張貼留言