第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否可以僅使用JavaScript將數(shù)據(jù)寫入文件?

是否可以僅使用JavaScript將數(shù)據(jù)寫入文件?

慕尼黑8549860 2019-06-16 13:30:41
是否可以僅使用JavaScript將數(shù)據(jù)寫入文件?我希望使用JavaScript將數(shù)據(jù)寫入現(xiàn)有文件。我不想把它打印在控制臺上。我想把數(shù)據(jù)寫到abc.txt..我讀了許多答案的問題,但每一個地方,他們都打印在控制臺上。在某些地方,他們給出了代碼,但它不起作用。因此,請任何人能幫助我如何實際寫入數(shù)據(jù)到文件。我引用了代碼,但它不起作用:它給出了錯誤: Uncaught TypeError: Illegal constructor鉻和 SecurityError: The operation is insecure.論莫茲拉var f = "sometextfile.txt";writeTextFile(f, "Spoon")writeTextFile(f, "Cheese monkey")writeTextFile(f, "Onion") function writeTextFile(afilename, output){   var txtFile =new File(afilename);   txtFile.writeln(output);   txtFile.close();}那么,我們真的可以只使用Javascript或不使用Javascript將數(shù)據(jù)寫入文件嗎?請?zhí)崆皫臀抑x謝
查看完整描述

3 回答

?
慕尼黑的夜晚無繁華

TA貢獻(xiàn)1864條經(jīng)驗 獲得超6個贊

對此有一些建議-

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


查看完整回答
反對 回復(fù) 2019-06-16
?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗 獲得超4個贊

可以在瀏覽器中使用BlobURL.createObjectURL..所有最近的瀏覽器支持這個.

您不能直接保存您創(chuàng)建的文件,因為這會導(dǎo)致大量的安全問題,但是您可以為用戶提供下載鏈接。您可以通過download屬性在支持下載屬性的瀏覽器中。與任何其他下載一樣,下載文件的用戶將對文件名擁有最終發(fā)言權(quán)。

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;
  };

這是一個,它使用此技術(shù)將任意文本從textarea.

如果希望立即啟動下載,而不要求用戶單擊鏈接,則可以使用鼠標(biāo)事件模擬鼠標(biāo)單擊鏈接。昂船洲回答做。我創(chuàng)造了一個更新示例使用這種技術(shù)。

  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);


查看完整回答
反對 回復(fù) 2019-06-16
?
神不在的星期二

TA貢獻(xiàn)1963條經(jīng)驗 獲得超6個贊

如果您說的是瀏覽器javascript,出于安全原因,您不能將數(shù)據(jù)直接寫入本地文件。HTML 5新API只能允許您讀取文件。

但是如果您想要寫入數(shù)據(jù),并允許用戶以文件的形式下載到本地。以下代碼起作用:

    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');



查看完整回答
反對 回復(fù) 2019-06-16
  • 3 回答
  • 0 關(guān)注
  • 2213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號