jQuery Tutorial to Download Files From Server in PHP Using AJAX Post Request in Browser Using Javascript Full Project For Beginners

 

 

GitHub

View project on GitHub or download the latest release.

npm

npm install gasparesganga-jquery-ajax-downloader

Bower

bower install gasparesganga-jquery-ajax-downloader

CDN
https://cdn.jsdelivr.net/npm/gasparesganga-jquery-ajax-downloader@1.1.0/src/ajaxdownloader.min.js

 

 

Examples

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

Example 1 – Static File

$.AjaxDownloader({
    url  : "./static_file.zip"
});

Example 2 – Dynamic Request

$.AjaxDownloader({
    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:

<?php
    /*
        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));
    ob_clean();
    flush();
    readfile($filepath);
    exit();
?>

 

See also  Avgrund CSS3 and Javascript UI Modal/Popup Window Library With Examples Full Source Code

 

jQuery AjaxDownloader

A jQuery Plugin to perform Ajax style downloads


Documentation and examples at http://gasparesganga.com/labs/jquery-ajax-downloader/

 

 

 

DOWNLOAD FULL SOURCE CODE

Leave a Reply