views:

34

answers:

1

Hi all,

I am using $ajax( { type:"post", url:"/whatever/" } ) to access my server which works nicely. The problem is that the response is not evaluated as I need it. On some occations the server sends back a dummy string ("x") which can be ignored on the browser, but on other occasions my server sends back a special file as attachment:

mimetype application/x-my-special-type
Content-Disposition attachment; filename=myname.ext
....

But the browser (I am only interested in Google Chrome / Chromium - it's an intranet thing) does not show the download popup window. (Calling the "/whatever/" URL directly in the address bar actually DOES show the download popup window to save the file.)

How can I persuade jQuery to show the usual download popup window?

+2  A: 

Send the user to the URL by setting window.location.href or linking directly to the file. Do not use AJAX for this, as it is only used for retrieving data to variables/inserting to DOM. You'll probably need to think of a different way to represent the no-response case. Perhaps the AJAX portion could be used to establish if a file is available or not, and the redirection would occur if the AJAX response indicates that a file is available.

spender
I was afraid to read that. Seems that I must send the ajax request, check the answer from the server, and if it contains something like "download_waiting" then do the window.location.href to actually do the download :(
mawimawi
Indeed. Updated my answer to say exactly that as you entered your comment.
spender
The no response could probably be handled by a 404 page, or a similar "The requested file was not found" type page.
Ryan Kinal