Php download file with ajax






















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. 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. Joao Marcos solution works for me but I had to modify the code to make it work on IE, below if what the code looks like. This solution is not very different from those above, but for me it works very well and i think it's clean.

Your needs are covered by window. But I am referring to a page that must first be processed and then downloaded. 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. Asked 7 years, 11 months ago. Active 7 months ago. Viewed k times. Thank you in advance. Improve this question. This will not work, see [this question][1]. Do location.

When you need this, it does feel like it's a common thing to ask for, and sadly with no elegant solutions. Add a comment. Active Oldest Votes. Original answer You can't have an AJAX request open the download prompt since you physically have to navigate to the file to prompt for download.

Improve this answer. Community Bot 1 1 1 silver badge. Steven Lambert Steven Lambert 5, 1 1 gold badge 26 26 silver badges 44 44 bronze badges.

Doesn't this call the link twice? I'm in a similar boat I'm passing a lot of security information in headers, and able to parse the file object in the success function, but don't know how to trigger a download prompt. It does call the page twice, so if you are querying a database in that page, this means 2 trips to DB. Let me explain how this helped me I have an ajax function that does some error checking on a form submission and then creates a csv file.

If the error check fails, it has to come back with why it failed. If it creates the CSV it is telling the parent that "go ahead and fetch the file". But it will send request 2 times, that is not proper — Dharmendrasinh Chudasama. Show 4 more comments. Taha I tested this on Edge and it seemed to work. Don't know about IE though.

My client doesn't target IE users ;- Am I lucky? You certainly want to hide it at the same time. Jelle Kralt Jelle Kralt 6 6 silver badges 15 15 bronze badges. The programming language you're using to change window. 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. 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 can put a link on the page with the URL to your file returned from the ajax call or another way is to use a hidden iframe and set the URL of the source of that iframe dynamically.

This way you can download the file without refreshing the page. 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? I can't set window. Muthu Kumaran 17k 5 5 gold badges 43 43 silver badges 70 70 bronze badges. No page reload. Can you give a little more detail and provide some code on what happens on both ends?



0コメント

  • 1000 / 1000