Client browser displays file download popup dialog and allows user to download the file; Our application is ajax based application, so it would be very easy and convenient for us to send ajax request (like using jquery.ajax function).
I have a button and onclick
it will call an ajax function.
Jquery.fileDownload.js Library jQuery File Download is a cross server platform compatible jQuery plugin that allows for an Ajax-like file download experience that isn’t normally possible using the web. Download File Using Javascript/jQuery. JQuery Ajax call for PDF file download. JavaScript/jQuery to download file via POST with JSON data.
Here is my ajax function
Although the integration services installed properly, there is a yellow exclaimation on the Virtual Machine Bus driver in Vista. I can get network connectivity through the legacy driver. Virtual drivers for windows 10.
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?
theking963theking963AJAX isn't for downloading files. Pop up a new window with the download link as its address, or do document.location = ..
.
A very simple solution using jQuery:
on the client side:
and on the server side, make sure you send back the correct Content-Type
header, so the browser will know its an attachment and the download will begin.
@joe : Many thanks, this was a good heads up!
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):
Initial post 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:
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.
joejoeYou 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