You can use this technique to download any kind of files. What server-side language are you using? This will in fact 'redirect' the browser to this download page, but as ahren alread said in his comment, it won't navigate away from the current page.
It's all about setting the correct headers so I'm sure you'll find a suitable solution for the server-side language you're using if it's not PHP. The server then generates a link from where this file can be downloaded, e. For example, the server responds with:. When processing the response, you inject an iframe in your body and set the iframe 's SRC to the URL you just received like this using jQuery for the ease of this example :.
If you've set the correct headers as shown above, the iframe will force a download dialog without navigating the browser away from the current page. After you've received the JSON response, you can then decide client-side what to do with it.
Maybe, for example, later on you want the user to click a download link to the URL instead of forcing the download directly, in your current setup you would have to update both client and server-side to do so. I see you've already found out a solution, however I just wanted to add some information which may help someone trying to achieve the same thing with big POST requests.
I had the same issue a couple of weeks ago, indeed it isn't possible to achieve a "clean" download through AJAX, the Filament Group created a jQuery plugin which works exactly how you've already found out, it is called jQuery File Download however there is a downside to this technique.
In slow Internet connections you'll have to wait a lot until the request is sent and also wait for the file to download. But as I discovered in my own app, for bigger file sizes it is almost unbearable.
My app allow users to export images dynamically generated, these images are sent through POST requests in base64 format to the server it is the only possible way , then processed and sent back to users in form of. In slow Internet connections it can be really annoying. My solution for this was to temporary write the file to the server, once it is ready, dynamically generate a link to the file in form of a button which changes between "Please wait This makes all the waiting time more bearable for users, and also speed things up.
Months have passed since I posted this, finally I've found a better approach to speed things up when working with big base64 strings. I now store base64 strings into the database using longtext or longblog fields , then I pass its record ID through the jQuery File Download, finally on the download script file I query the database using this ID to pull the base64 string and pass it through the download function. I know this is way beyond what the OP asked, however I felt it would be good to update my answer with my findings.
For those looking a more modern approach, you can use the fetch API. The following example shows how to download a spreadsheet file. It is easily done with the following code. Also, it has a similar syntax to the jQuery approach, without the need to add any additional libraries. Of course, I would advise checking to which browser you are developing, since this new approach won't work on IE.
You can find the full browser compatibility list on the following [link][1]. This url must be set, on my example I am assuming you know this part. Also, consider the headers needed for your request to work.
I want to point out some difficulties that arise when using the technique in the accepted answer, i. You can't set headers on the request.
If your authentication schema involves headers, a Json-Web-Token passed in the Authorization header, you'll have to find other way to send it, for example as a query parameter.
You can't really tell when the request has finished. Well, you can use a cookie that gets set on response, as done by jquery. It won't work for concurrent requests and it will break if a response never arrives.
You can only use the content types supported by a form. Which means you can't use JSON. I ended up using the method of saving the file on S3 and sending a pre-signed URL to get the file. As others have stated, you can create and submit a form to download via a POST request. However, you don't have to do this manually.
One really simple library for doing exactly this is jquery. It provides an API similar to the standard jQuery. This is a 3 years old question but I had the same problem today. I looked your edited solution but I think that it can sacrifice the performance because it has to make a double request.
So if anyone needs another solution that doesn't imply to call the service twice then this is the way I did it:.
This form is just used to call the service and avoid to use a window. After that you just simply have to make a form submit from jquery in order to call the service and get the file. It's pretty simple but this way you can make a download using a POST. I now that this could be easier if the service you're calling is a GET , but that's not my case. I used this FileSaver. In my case with csv files, i did this in coffescript :.
I think for most complicated case, the data must be processed properly. Under the hood FileSaver. To get Jonathan Amends answer to work in Edge I made the following changes:. Below is my solution for downloading multiple files depending on some list which consists of some ids and looking up in database, files will be determined and ready for download - if those exist.
There is absolutely no need to use ajax for this at all. Just call window. All this is doing is making the same request twice. I have no idea why this was upvoted. Show 4 more comments. Equals Constants. Combine Path. Message ; return Content e. GetExtension path. Raw Localizer["Report has been exported successfully"]. ReadAllBytes file1. FullName ; return File fileBytes, System. Dilshad Dilshad 1 1 1 bronze badge. A big block of code thrown up on the web without explanation doesn't help anyone.
You should explain your approach and why your answer is better or different from the other answers. AllowGet ; if sDocument. Contains ". Mohanavelu K Mohanavelu K 69 1 1 silver badge 3 3 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. In case you don't use FPDF and need to set it manually simply add this before the output:. The more interesting thing about this is how the file is downloaded after sending the HTTP request.
Let's dive straight into it:. The actual download is done by creating a Blob object, which is used for a newly created a tag with a link to the created Blob object which is automatically clicked which ultimately opens the "Save file" dialog. Additionally it's appended to the body which is a fix for Firefox and is removed from the body afterwards we don't want to have tons of invisible a tags on our body.
0コメント