Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download .txt using JavaScript without dialog prompt

Tags:

javascript

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" dialog ?

like image 749
user2405219 Avatar asked Sep 13 '26 00:09

user2405219


1 Answers

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();
}
like image 76
WiseDev Avatar answered Sep 15 '26 13:09

WiseDev