Gaspare Sganga

IT Supervisor, GIS Analyst & Lead Developer @setin.

Freelance Developer & Consultant.

Buy me a coffee?
Download View on GitHub

Quick Demo

How it works

An HTML form is created and submitted to ad invisible IFrame. You can pass parameters like you would in a regular Ajax request.
The server should answer the request providing a file to download of course.


The only method provided works like any other Ajax call made with jQuery, thus using the defaults provided with $.ajaxSetup().


Performs an ajax-style download

Options and defaults values

data   : $.ajaxSetup()["data"]    // Object
url    : $.ajaxSetup()["url"]     // String

Data to send to the server along the request (it acts the same way as jQuery.ajax() data parameter).


URL to which the request is sent (it can be a static file to download as well).


A couple of examples using both a static file and a dynamic request:

Example 1 - Static File

    url  : "./"

Example 2 - Dynamic Request

    url  : "./download_manager.php",
    data : {
        param1   : "xxx",
        param2   : "yyy",
        param3   : "zzz"

Server Side

Some care should be taken server side too. In my first example I requested a static file and it seemed to work just fine. But what would have happened with a PDF file or any other file format that your browser can actually open?
The solution is easy, just do some server side magic, like sending the right headers. Here is a PHP example to serve a static file forcing a download dialog on the user’s browser:

        Do some parameters checks, database data collection, etc. etc. here
    // Force a download dialog on the user's browser:
    $filepath = '/path/to/file.pdf';
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate');
    header('Content-Length: '.filesize($filepath));

Comments and Ideas

Want to leave a comment or give me an idea? Email me or use the comments section below.