I want to download a pdf file for jquery ajax response. Ajax response contains pdf file data. I tried this solution. My code is given below but I always get a blank pdf. 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. Download jQuery - jquery-3.3.1.js Download, jquery-2.2.4.js Download « Previous; Next » Before started jQuery, we need to download jQuery library file. Download latest version of jquery.js file from jquery.com Website. You can also use jQuery CDN link. CDNs can offer a performance benefit by hosting jQuery on servers spread across the global.
Active2 years, 6 months ago
How to download a file through ajax request in asp.net MVC 4. Basically if we don't give a ajax call still we can download file through. JavaScript/jQuery to. Hi, From Jquery ajax i am making a C# function call which is returing a file download option. If i am using $.ajax, it is not working, if i am giving window.location = url then that download box is coming.
I have a button and
onclick it will call an ajax function.
Here is my ajax function
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.php it outputs the contents of the file.csv into the page(into the ajaxDiv) instead of downloading.
Is there a way to force download the file at the end of csv.php?
theking963theking963
1,08055 gold badges2121 silver badges3939 bronze badges
5 Answers
AJAX isn't for downloading files. Pop up a new window with the download link as its address, or do Marc BMarc B
document.location = .. .
322k3131 gold badges340340 silver badges439439 bronze badges
A very simple solution using jQuery:
Javascript Download File Using Ajax
on the client side:
and on the server side, make sure you send back the correct Muthu Kumaran
Content-Type header, so the browser will know its an attachment and the download will begin.
15.2k55 gold badges3535 silver badges6161 bronze badges
blink281blink281
@joe : Many thanks, this was a good heads up!
Windows xp emulator free. I had a slightly harder problem:1. sending an AJAX request with POST data, for the server to produce a ZIP file2. getting a response back3. download the ZIP file
So that's how I did it (using JQuery to handle the AJAX request):
The 'request/url' handles the zip creation (off topic, so I wont post the full code) and returns the following JSON object. Something like:
The 'request/download' can perform some security checks, if needed, and generate the file transfer:
DarioDario
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:
Server slurps the file, unlinks it, and sends the stream to the client, with these headers:
![]()
Works like a charm.
joejoe
60811 gold badge99 silver badges1616 bronze badges
You can't download the file directly via ajax.
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.
Here is the code
Naimish B. MakwanaNaimish B. Makwana
Not the answer you're looking for? Browse other questions tagged phpjavascriptajaxdownload or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |