Click here to Skip to main content
14,665,013 members
Rate this:
Please Sign up or sign in to vote.
See more:
My Script:
            var myTrigger;
            var progressElem = $('#progress');
                type: 'GET',
                url: 'download.php',
                beforeSend: function(thisXHR)
                    myTrigger = setInterval(function()
                        if (thisXHR.readyState > 2)
                            var totalBytes = thisXHR.getResponseHeader('Content-length');
                            var dlBytes = thisXHR.responseText.length;
                            (totalBytes > 0) ? progressElem.html(Math.round((dlBytes / totalBytes) * 100) + "%") : progressElem.html(Math.round(dlBytes / 1024) + "K");
                    }, 200);
                complete: function()
                success: function(response)
                    // Process XML


        <form  enctype="multipart/form-data">
            <iframe style="display: none"></iframe>
            <div id="progress" style="width:500px;border:1px solid #ccc;"></div>
            <!-- Progress information -->
            <div id="information" style="width"></div>
            <input type="text" name="file">
            <button id="download" >Download</button>
     <!--<input type="file" name="file" id="file"><br>
     <input type="submit" name="submit" value="Submit">-->




// Sanitizing the filename:
$filename = $_POST['file'];

// Outputting headers:
header("Cache-Control: ");
header("Content-type: application/octet-stream");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filename));
header('Content-Disposition: attachment; filename="'.$filename.'"');


I don't know how to call this JavaScript in my "download" button. And I don't know how to calculate total size of my file and how many bytes are downloaded in client pc.

It will helpful for me if you make my code as required it have to do.
Prasad Khandekar 8-Apr-13 10:11am
You can not use AJAX for download.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

let the browser handle its download issue. and use the below code:

<button id="download" onclick='window.location="download.php?file=filename"'>Download</button>

And from your download.php use $_GET method not $_POST.

you can also try POST method with ajax, but if I try to give you proper method then i will have to test it first. what you can do is(as test) create a div as hidden with some


success: function(response)
   //my jquery can be wrong but you can look for the right one
   $(document).append("<div id="SomeCrazyId" style="display:none"></div>");

and see what happen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100