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. Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search.
I create the csv file based on the user input. After it's created I want it to prompt download or force download preferably force. I am using the following script at the end of the php file to download the file.
If I run this script in a separate file it works fine. But If I run it at the end of csv. AJAX isn't for downloading files. Pop up a new window with the download link as its address, or do document. I had a slightly harder problem: 1. Something like:. I have accomplished this with a hidden iframe. I use perl, not php, so will just give concept, not code solution. Client sends Ajax request to server, causing the file content to be generated. This is saved as a temp file on the server, and the filename is returned to the client.
Client javascript receives filename, and sets the iframe src to some url that will deliver the file, like:. You're right mikemaccana, I actually meant ajax :. Have been hunting high and low for a solution and this is so elegant and perfect.
Thank you so much. Of course, this solution will only work if it is a static file that already exists. If the server responds with an error though there won't be any way to stay on your main page without being redirected to an error page by the browser.
At least this is what Chrome does when the result of window. Show 1 more comment. Good Generic Code. Thanks leo. Its helpful.
Also what do you suggest adding window. The filename will be wrong if the content disposition specifies a non-UTF8 filename. So, after I get the responseText and everything is Ok, I redirect browser like this: window. Pedro Sousa Pedro Sousa 6 6 silver badges 11 11 bronze badges.
Isn't this dangerous security-wise? I would think so because anybody can call download. Since the access has reached a PHP script, htaccess now stops its duty. One should always sanitise the requested file to be read — Prof. Show 2 more comments. I prefer location. Telmo Dias Telmo Dias 3, 2 2 gold badges 31 31 silver badges 42 42 bronze badges. Alain Cruz Alain Cruz 3, 3 3 gold badges 19 19 silver badges 37 37 bronze badges.
Jemil Oyebisi Jemil Oyebisi 7 7 silver badges 8 8 bronze badges. Decoding a filename from the header is a little bit more complex Jaime 4, 2 2 gold badges 19 19 silver badges 42 42 bronze badges. Lumic Lumic 41 2 2 bronze badges. Please format your entire code block and provide some additional explanation to your process for future reader benefit. You can just make the a tag hidden and populate the href dynamically. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Download file through an ajax call php Ask Question. Asked 10 years, 4 months ago. Active 4 years, 8 months ago. Viewed k times. I have a button and onclick it will call an ajax function. Is there a way to force download the file at the end of csv. Improve this question. Add a comment. Active Oldest Votes.
Improve this answer. Marc B Marc B k 39 39 gold badges silver badges bronze badges. I believe technically it's better to set window. Why is AJAX not for downloading files?
0コメント