10/30/2019 Download File Using Ajax
Downloading files from Ajax POST Requests Occasionally I stumble upon the need to download files from POST requests. An example would be generating PDF files, where the PDF content is dependent on the request. You can't directly return a file for download via an AJAX call so, an alternative approach is to to use an AJAX call to post the related data to your server. You can then use server side code to create the Excel File (I would recommend using EPPlus or NPOI for this although it. It may have its own reasons like showing the download progress of the file in the applicaton's UI itself. Another reason may be monetization - the application can show an advertisement to the user while the file is being downloaded. This tutorial shows how to make an AJAX request to download a file, and showing the download percentage completed. Javascript - Downloading Files with AJAX and Showing a Progess Bar Currently most of the web applications show a normal link for the users to download a file. When the link is clicked, the browser detects that it is a file, and allows the user to download it.
Active4 months ago
I want to send an 'ajax download request' when I click on a button, so I tried in this way:
javascript:
download.php:
but doesn't work as expected, how can I do ? Thank you in advance
Manuel Di IorioManuel Di Iorio
1,20933 gold badges1818 silver badges2727 bronze badges
10 Answers
Update April 27, 2015
Up and coming to the HTML5 scene is the download attribute. It's supported in Firefox and Chrome, and soon to come to IE11. Depending on your needs, you could use it instead of an AJAX request (or using
window.location ) so long as the file you want to download is on the same origin as your site.
You could always make the AJAX request/
window.location a fallback by using some JavaScript to test if download is supported and if not, switching it to call window.location .
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. Instead, you could use a success function to navigate to download.php. 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.location and avoid the AJAX request entirely.
Community♦
Steven LambertSteven Lambert
You actually don't need ajax at all for this. If you just set 'download.php' as the href on the button, or, if it's not a link use:
The browser should recognise the binary download and not load the actual page but just serve the file as a download.
Jelle KraltJelle Kralt
To make the browser downloads a file you need to make the request like that:
João MarcosJoão Marcos
2,98311 gold badge1414 silver badges1414 bronze badges
Cross browser solution, tested on Chrome, Firefox, Edge, IE11.
In the DOM, add an hidden link tag:
Then:
leoleo
91811 gold badge1515 silver badges2020 bronze badges
It is possible. You can have the download started from inside an ajax function, for example, just after the .csv file is created.
I have an ajax function that exports a database of contacts to a .csv file, and just after it finishes, it automatically starts the .csv file download. So, after I get the responseText and everything is Ok, I redirect browser like this:
My download.php file looks like this:
There is no page refresh whatsoever and the file automatically starts downloading.
NOTE - Tested in the following browsers:
Pedro SousaPedro Sousa
Download File Using Ajax Laravel
I prefer
location.assign(url);
Complete syntax example:
Telmo DiasTelmo Dias
Decoding a filename from the header is a little bit more complex..
LumicLumic
This solution is not very different from those above, but for me it works very well and i think it's clean. App download android.
I suggest to base64 encode the file server side (base64_encode(), if you are using PHP) and send the base64 encoded data to the client
On the client you do this:
This code puts the encoded data in a link and simulates a click on the link, then it removes it.
martinethylmartinethyl
Your needs are covered by
window.location('download.php'); But I think that you need to pass the file to be downloaded, not always download the same file, and that's why you are using a request, one option is to create a php file as simple as showfile.php and do a request like Buy gold miner pc game.
showfile.php
where file is the file name passed via Get or Post in the request and then catch the response in a function simply
Ajax Call To Download File
lisandrolisandro
there is another solution to download a web page in ajax. But I am referring to a page that must first be processed and then downloaded.
First you need to separate the page processing from the results download.
1) Only the page calculations are made in the ajax call.
I hope this solution can be useful for many, as it was for me.
netlukenetluke
Not the answer you're looking for? Browse other questions tagged javascriptphpajaxfiledownload or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |