3 回答

TA貢獻(xiàn)1864條經(jīng)驗 獲得超6個贊
如果您試圖在客戶端計算機(jī)上編寫文件,則不能以任何跨瀏覽器的方式這樣做。IE確實有允許“受信任的”應(yīng)用程序使用ActiveX對象來讀取/寫入文件的方法。 如果您試圖將其保存在您的服務(wù)器上,那么只需將文本數(shù)據(jù)傳遞給您的服務(wù)器,并使用某種服務(wù)器端語言執(zhí)行文件編寫代碼。 要在客戶端存儲一些相當(dāng)小的信息,可以選擇cookie。 使用HTML 5 API進(jìn)行本地存儲。

TA貢獻(xiàn)1752條經(jīng)驗 獲得超4個贊
Blob
URL.createObjectURL
download
var textFile = null, makeTextFile = function (text) { var data = new Blob([text], {type: 'text/plain'}); // If we are replacing a previously generated file we need to // manually revoke the object URL to avoid memory leaks. if (textFile !== null) { window.URL.revokeObjectURL(textFile); } textFile = window.URL.createObjectURL(data); // returns a URL you can use as a href return textFile; };
textarea
.
var create = document.getElementById('create'), textbox = document.getElementById('textbox'); create.addEventListener('click', function () { var link = document.createElement('a'); link.setAttribute('download', 'info.txt'); link.href = makeTextFile(textbox.value); document.body.appendChild(link); // wait for the link to be added to the document window.requestAnimationFrame(function () { var event = new MouseEvent('click'); link.dispatchEvent(event); document.body.removeChild(link); }); }, false);

TA貢獻(xiàn)1963條經(jīng)驗 獲得超6個贊
function download(strData, strFileName, strMimeType) { var D = document, A = arguments, a = D.createElement("a"), d = A[0], n = A[1], t = A[2] || "text/plain"; //build download link: a.href = "data:" + strMimeType + "charset=utf-8," + escape(strData); if (window.MSBlobBuilder) { // IE10 var bb = new MSBlobBuilder(); bb.append(strData); return navigator.msSaveBlob(bb, strFileName); } /* end if(window.MSBlobBuilder) */ if ('download' in a) { //FF20, CH19 a.setAttribute("download", n); a.innerHTML = "downloading..."; D.body.appendChild(a); setTimeout(function() { var e = D.createEvent("MouseEvents"); e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); a.dispatchEvent(e); D.body.removeChild(a); }, 66); return true; }; /* end if('download' in a) */ //do iframe dataURL download: (older W3) var f = D.createElement("iframe"); D.body.appendChild(f); f.src = "data:" + (A[2] ? A[2] : "application/octet-stream") + (window.btoa ? ";base64" : "") + "," + (window.btoa ? window.btoa : escape)(strData); setTimeout(function() { D.body.removeChild(f); }, 333); return true;}
download('the content of the file', 'filename.txt', 'text/plain');
添加回答
舉報