views:

704

answers:

5

I am being asked to make a "download" button that downloads the contents of a textarea on the same page as a file, with the browser's "Save As" dialog showing up. Copy/paste would do the job just fine, but it is a "requirement".

Right now, I am just posting the contents of the textarea to the server, which echos them back with "Content-disposition: attachment" slapped on. Is there a way to do this with just client-side Javascript?

+2  A: 

Short answer: it's not posible. You have to POST it to server, and response from server can be "Content-disposition: attachment".

firem
+1  A: 

You could try window.location = "data:application/octet-stream,"+text but that doesn't provide a mechanism through which you can suggest a name, and also IE has a very small cap on the maximum length of a data URI which could be a problem.

olliej
And it's not even possible with IE6.
Rakesh Pai
cool. http://en.wikipedia.org/wiki/Data_URI_scheme
Thilo
A: 

Try creating a web service and than call it using AJAX

Jomit
A: 

It might be possible by creating a frame, writing contents there, then calling document.execCommand('saveas', ...) in IE and something with nsIFilePicker in Mozilla, but I believe that would require some extraordinary privileges (like being part of the browser itself).

Andrey Shchekin
+1  A: 

There were some javascript libraries that did this kind of thing, via small embedded SWF file. For example this one.

yacoob