Download .txt using JavaScript without dialog prompt

后端 未结 2 1565
失恋的感觉
失恋的感觉 2020-12-22 12:36

Is it possible to create and download a .txt file using only JavaScript (no server-side programming !), and save it on local drive, without displaying browser \"Save file\"

相关标签:
2条回答
  • 2020-12-22 13:29

    Rickard Staaf's answer is outdated. To download a file in javascript locally without prompting a dialog box, be sure to enable it in your browser settings (chrome >> settings >> advanced >> downloads and turn off 'Ask where to save each file before downloading'.

    Subsequently, you can write a simple text file like so using blob objects:

    function save() {
      var content = ["your-content-here"];
      var bl = new Blob(content, {type: "text/plain"});
      var a = document.createElement("a");
      a.href = URL.createObjectURL(bl);
      a.download = "your-download-name-here.txt";
      a.hidden = true;
      document.body.appendChild(a);
      a.click();
    }
    
    0 讨论(0)
  • 2020-12-22 13:30

    No not without browser plugins, that would be a big security risk.

    0 讨论(0)
提交回复
热议问题