Kick off file download with XHR request

derekl
derekl used Ask the Experts™
on
I'm writing a fairly complex web application that needs to support the download of data in CSV format.  I already have a servlet I can query which expects a POST request with JSON in the body describing the data that I want that returns JSON data in response to an XMLHttpRequest.

What I would like to do is duplicate that XmlHttpRequest (against a different servlet URL) which returns the same data in CSV format and would cause the save file dialog to open.

My fear is that the browsers normal response to a "text/plain" MIME type will be suppressed by virtue of the fact that the request originated with XmlHttpRequest.

Is there any way to do what I need?

Another way to do this would be to create an arbitrary POST request (with JSON data as the body) and redirect the browser to it, but I have no idea how to do this.

I'm lucky in that this is a Firefox only app so solutions need not be portable.  Thanks in advance.

Derek
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Big Data Engineer
Commented:
http://support.microsoft.com/kb/260519
Add the following header to your response when you want to download:
Content-disposition: attachment; filename=fname.ext

To redirect your browser to that page, use window.open()
http://www.javascript-coder.com/window-popup/javascript-window-open.phtml

No reason why this solution won't even be portable :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial