Ajax to download file
URL window. Browser Compatibility. View Demo. 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. You can't have an AJAX request open the download prompt since you physically have to navigate to the file to prompt for download.
Instead, you could use a success function to navigate to download. This will open the download prompt but won't change the current 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. 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. For this, we need to create a link on the page so that either the user can click it, or, in our case, the script can click the link. As we can see from the documentation, the URL.
Luckily for us, we can instruct Axios to request the file as a blob using the following configuration:. From the documentation, we also learned that it is a good practice to revoke the URL so that we release the used up memory.
Next up, we need to create the hyperlink for the file so that it can be clicked, first, we create the anchor element dynamically, provide it the URL, attach it to the DOM, click it, and then we remove the element so that we clean up after ourselves.
0コメント