1

I am posting some data to the server using jquery ajax. The server spews out a file (if the request is successful). I am just wondering how to initiate the save file dialog within the success callback given the results? Thanks.

PS:

The simplest solution is:

$('#ExportToExcel').click(function () {
                $('#form').attr({ action: 'bla' });
                $('#form').submit();
                return false;
            });
5
  • You mean to ask how to save a local file using javascript (which you can't, as far as I know)? Your question is not very clear. Commented Mar 16, 2011 at 11:49
  • the server produces the file depending on the posted data. Commented Mar 16, 2011 at 11:54
  • please use a more descriptive title in the future. This will help others looking for the same to find the answer quicker. Commented Mar 16, 2011 at 11:55
  • @aron your solution works (can you repost it as you deleted your answer)? Please note, however, that i do not need to save the file first and send the uri back as i stream the data. thanks anyway! Commented Mar 16, 2011 at 12:35
  • This question is similar to: How to make a browser display a "save as dialog" so the user can save the content of a string to a file on his system?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Apr 28 at 12:26

2 Answers 2

10

It is possible to ask the browser to offer the Save File dialog in your situation, yes, with a combination of JavaScript, the server response, and the handling of that response by the browser.

Here's how:

  1. Create a form to POST the data to the server.
  2. Create a hidden iframe with a unique name.
  3. Set the form's target to be the name of the iframe.
  4. Send the form (e.g., formElement.submit()).
  5. Have your server respond using the Content-Disposition header with the value attachment. This is a suggestion to the browser that the result should be saved rather than displayed in the browser itself.

If you want to get really fancy, you can use the cookie trick I describe in this other answer so you can get notification when the response is received (whether the server responds with the requested data, or an error). I use that very successfully in a browser-based DHML+JavaScript application where the user can request a custom PDF, and choose to either view it in a browser window (in which case I have the server include the Content-Disposition: inline header) or save it as a file (Content-Disposition: attachment).

Sign up to request clarification or add additional context in comments.

7 Comments

Can you please let me know how to this: "Set the form's target to be the name of an iframe." Please note I am using the same form for filtering a grid.
can you please have a look at the my not working iframe solution (see PS of question)? Thanks.
@csetzkom: You can't do it via $.ajax at all, that's what I meant with steps 1-4 above. You literally fill in fields in a form (they can be hidden fields if you like) and use the submit method of the form's DOM element to do it. This doesn't tear down your page the way a form submit usually would, because the form has a target. You use the cookie trick to know when the response has been received. The user experience is largely the same as your $.ajax solution, it's just how you make it happen that's different.
I am using the same form to filter my grid so I think the 'target solution' would not work. Or should I use a hidden form for the exporttoexcel button (after pressing this button the the user gets the data as excel file depending on the entered form values). I will give it another go tomorrow. Thanks so far.
@csetzkorn: No reason not to use a form specifically for this purpose. It doesn't even have to pre-exist in the markup, you can create it on-the-fly.
|
2

You can't open a save file dialog with pure JavaScript.

EDIT: it seems you can. See T.J. Crowder's answer.

Original answer is still valid:

You could use Flash, or simply redirect to the download page using JavaScript.

For example:

$.post('/some/url', function(data) {
    // lets for the moment, assume that you 
    // return a JSON string containing some
    // parameters, data.url being the URL of 
    // your download file
    var downloadUrl = data.url;
    window.location.href = downloadUrl;
});

It is not possible to download the file that is being returned directly by your AJAX-request. Instead, save the file on the server - perhaps in a temporary file - and return a URL that downloads the file and removes it from the server afterwards.

4 Comments

Can I redirect to a URL whilst posting the data in the form? That would be ideal I think. Thanks.
@Csetz I don't understand what you mean. Could you rephrase your question?
Actually, it is possible to use JavaScript plus the server response to do what the OP's described.
@csetzkorn Just return HTTP redirect code, something like 303?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.