Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript save data to file system (with user prompt)

What is the best way to achieve the below in latest latest browsers (with html5 support). I mainly target Google Chrome.

In my application, data is manipulated through javascript and needs to write output to the file system with a browser prompt (save as dialog). I am not sure about the security restrictions to write to file system, but I am not planning anonymous write (but user is prompted, and selects the location).

I see saveAs not natively supported yet. With my research, I see few options.

  1. https://github.com/eligrey/FileSaver.js
  2. a.download (http://html5-demos.appspot.com/static/a.download.html)
  3. web filesystem
  4. object URLs

FileServer falls back to 2,3,4 if no native support and I may use it. But, I don't find a way to open a Save As dialog. It just save a file in default location (downloads folder in mac).

Which option would you use to get a good support in latest browsers? How can get open the Save As dialog and let the user name the file.

Thanks.

like image 405
bsr Avatar asked Mar 04 '13 22:03

bsr


1 Answers

I agree with Nathan Hayfield. The browser will handle the save dialog box for you. Setting the header Content-Disposition will send the response back as an attachment and the browser will handle it from there. eg.

content.Headers.Add("Content-Disposition", "attachment; filename=export.csv");
like image 169
Shane Calhoun Avatar answered Oct 29 '22 03:10

Shane Calhoun