I’m using AJAX to submit a POST request to my Rails controller
$("#submit_form").submit(function(event) {
...
/* Send the data using post*/
$.post( "/download", { s: term },
function( data ) {}
);
});
The controller returns a file using the Rails send_file method. However, when I click on the form button, no file is downloaded to me, even though the response header says
Content-Disposition:attachment; filename="pic.jpg"
How can I get the file?
It’s AFAIK not possible to let users download files using AJAX requests and I see no reason to do it that way, since the user will remain on the page when downloading a file.
For a file to be downloaded, the response must be handled by the web browser rather than your JavaScript code.