Download file through ajax call






















Related Articles. Add Comments. Thank you for the feedback. The comment is now awaiting moderation. You will be notified via email when the author replies to your comment.

Please select a comment to reply. You can add your comment about this article using the form below. Make sure you provide a valid email address else you won't be notified when the author replies to your comment Please note that all comments are moderated and will be deleted if they are Not relavant to the article Spam Advertising campaigns or links to other sites Abusive content. Please do not post code, scripts or snippets. Required Invalid Email Address.

Security code:. Required Invalid security code. I declare, I accept the site's Privacy Policy. This is what i was looking for, 1- block the button and show processing so that the user can have a feedback that things are happening. Sorry my English. No need to create temporary files on the server. On jQuery v2. Mike S Mike S 2 2 silver badges 7 7 bronze badges. To get the file name from Content-Disposition, this match worked for me: filename.

However, your solution was the only solution that worked after searching a lot. Here is another lazier way to fetch the filename : npmjs. Wai Ha Lee 7, 61 61 gold badges 56 56 silver badges 85 85 bronze badges. EL missaoui habib EL missaoui habib 1, 1 1 gold badge 14 14 silver badges 24 24 bronze badges. Could you explain your answer?

That'd help others understand what you've done so they could apply your techniques to their situations. Just a warning: Safari and IE does not support the download attribute, so your file will end up having the name "Unknown" — Yangshun Tay. Quick question, won't this generate the file twice? Once You send the ajax request. Then you make the page redirect to the same URL as well. How can we eliminate that? Not in my case. I only tested it on Chrome though. As coderhs already states correctly, the action gets called twice.

It's getting called twice for me too. Your server-side code should also have something like this: res. Andrew Koster Andrew Koster 1, 1 1 gold badge 15 15 silver badges 26 26 bronze badges.

M46 M46 6 6 silver badges 18 18 bronze badges. I'm returning a zip file but when I use this method my zip file is invalid. I had to remove dataType from the ajax call to get it to work. I've changed my code. Ok so here is the working code when Using MVC and you are getting your file from a controller lets say you have your byte array declare and populate, the only thing you need to do is to use the File function using System. Octet, "nameoffile.

Yannick Richard Yannick Richard 1, 14 14 silver badges 25 25 bronze badges. Kit Ramos Kit Ramos 1, 1 1 gold badge 13 13 silver badges 30 30 bronze badges.

Shahrukh Alam Shahrukh Alam 11 3 3 bronze badges. Code only answers should have at least a minumum description explaining how the code works and why it answers to the question. Pierre Pierre 6, 4 4 gold badges 50 50 silver badges 69 69 bronze badges. Alireza Fattahi Alireza Fattahi 36k 12 12 gold badges silver badges bronze badges. It is certain that you can not do it through Ajax call. However, there is a workaround.

Steps : If you are using form. Upon "success" being returned from server, call your form. Aman Srivastava Aman Srivastava 1 1 silver badge 4 4 bronze badges. First you need to separate the page processing from the results download. For example, this can be a page that prints the results of a table calculated in the ajax call. I hope this solution can be useful for many, as it was for me. That's it works so fine in any browser I'm using asp. Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. 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 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. Even though this answers the question, it's better to just use window.

You actually don't need ajax at all for this. If you just set "download. The browser should recognise the binary download and not load the actual page but just serve the file as a download. It is possible. You can have the download started from inside an ajax function, for example, just after the. I have an ajax function that exports a database of contacts to a. So, after I get the responseText and everything is Ok, I redirect browser like this:.

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.

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.



0コメント

  • 1000 / 1000