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 ?
Asked
Active
Viewed 5,136 times
1
-
8Imagine a world where any webpage could write to your local file system without asking you... – David Sep 13 '14 at 18:56
-
Well, you can store texts in [localstorage](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage) or in a [sandboxed filesystem](https://developer.mozilla.org/en-US/docs/WebGuide/API/File_System), but not directly on a specific drive. – Bergi Sep 13 '14 at 19:00
-
1it's easy, others on here so far are wrong. use http://danml.com/download.html if you want to save many files, you have to confirm once after the 2nd or 3rd download, but then you can download all day without further clicks. – dandavis Sep 13 '14 at 19:20
-
1@Bergi - There is no sandboxed filesystem. The previous File System API has been [cancelled](http://lists.w3.org/Archives/Public/public-webapps/2014AprJun/0010.html) and should not be [referenced](http://dev.w3.org/2009/dap/file-system/pub/FileSystem/). – Derek 朕會功夫 Sep 13 '14 at 19:28
-
@dandavis: None of the examples there work for me without showing a file-save dialog. And it would be very unintuitive anyway. – Bergi Sep 14 '14 at 13:52
-
@Bergi: asking or auto-saving is a browser pref. i can download 5 files on that page with 5 clicks, and if i use setInterval, it downloads files all afternoon w/o intervention, at least in chrome... – dandavis Sep 14 '14 at 18:33
-
@Bergi: in chrome, the relevant setting is "Ask where to save each file before downloading", which should be un-checked to allow auto-downloading. In firefox, the pref appears on the download dialog itself: "Save File" + " () Do this automatically for files like this from now on". Once the pref is changed on either browser, downloading can proceed un-assisted by the human touch. – dandavis Sep 14 '14 at 18:51
2 Answers
8
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();
}

WiseDev
- 524
- 4
- 17