4

I am developing a download manager using PHP and jquery.

Requested a script that will download a file and also show the download progress.

I tried the following, but it not works

Jquery

function downloadFile(){
    var fileNameDownloading ="somefile.mp3"
    var oReq = new XMLHttpRequest();    
    oReq.addEventListener("progress", updateProgress, false);
    var params = "filename="+fileNameDownloading;   
    oReq.open("GET", "scripts/download.php?"+params, true);
    oReq.responseType = "blob";//blob arraybuffer

    function updateProgress (e) {
        console.log(e.loaded/e.total);
    }
    oReq.send();    
}

PHP

<?php
$file = $_GET['filename'];
$fileSize = get_size($file);
$packetSize = 262144;//2000
if($packetSize > $fileSize){
    $packetSize = $fileSize;
}
download($file,$packetSize);


function download($file,$chunks){
    set_time_limit(0);
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-disposition: attachment; filename='.basename($file));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    $size = get_size($file);
    header('Content-Length: '.$size);

    $i = 0;
    while($i<=$size){
        //Output the chunk
        get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks));
        $i = ($i+$chunks);
    }

}

//Callback function for CURLOPT_WRITEFUNCTION, This is what prints the chunk
function chunk($ch, $str) {
    
    print($str);
    return strlen($str);
}

//Function to get a range of bytes from the remote file
function get_chunk($file,$start,$end){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $file);
    curl_setopt($ch, CURLOPT_RANGE, $start.'-'.$end);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'chunk');
    $result = curl_exec($ch);
    curl_close($ch);
}

//Get total size of file
function get_size($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    return intval($size);
}
?>

Looking for a suggestion, how we can download a file and also show download progress using PHP and javascript. Thanks

Community
  • 1
  • 1
Ajit Kumar
  • 43
  • 1
  • 1
  • 6
  • You can't really do that. Once the browser starts downloading the file I believe you can't track it. And why should you? Every browser has some kind of progress indication going on? – php_nub_qq Jan 10 '14 at 02:45
  • This is not jQuery, it's pure javascript. – L105 Jan 10 '14 at 02:58
  • Hi php_nub_qq , The progress bar is to give all user uniform feeling in all browsers and in same screen where user clicked download, as different browsers have different downloading progress bars and at different places. – Ajit Kumar Jan 11 '14 at 02:58
  • Hi, L105. Yes this is pure javascript, I thought jquery can provide some better solution, so I kept jquery option open. – Ajit Kumar Jan 11 '14 at 03:03
  • Hi Ajit, was my answer able to help you with your question / did you get your question resolved? – jth_92 Apr 21 '14 at 14:48

2 Answers2

1

Two possible methods include using either polling or the uploadProgress ajaxSubmit option.

Using ajax / javascript polling, you would have a setTimeout every second that calls a server file and sees how much data has been written to the server. This article should help provide a good example and description of polling. The callback would set the progress based on the file size.

The second method is using the uploadProgress ajaxSubmit option. This article will give a good tutorial for how to set this up.

The following is a code example:

jQuery:

$(document).ready(function() {  
var options = { 
        target:   '#output', 
        beforeSubmit:  beforeSubmit,
        uploadProgress: OnProgress, //upload progress callback 
        success:       afterSuccess,
        resetForm: true  
    }; 
    
 $('#MyUploadForm').submit(function() { 
        $(this).ajaxSubmit(options);            
        return false; 
    });
});

function OnProgress(event, position, total, percentComplete)
{
    //Progress bar
    progressbar.width(percentComplete + '%') //update progressbar percent complete
    statustxt.html(percentComplete + '%'); //update status text
    if(percentComplete>50)
        {
            statustxt.css('color','#fff'); //change status text to white after 50%
        }
}

HTML:

<div id="upload-wrapper">
<div align="center">
<h3>Ajax Image Uploader with Progressbar</h3>
<span class="">Image Type allowed: Jpeg, Jpg, Png and Gif. | Maximum Size 1 MB</span>
<form action="processupload.php" onSubmit="return false" method="post" enctype="multipart/form-data" id="MyUploadForm">
<input name="ImageFile" id="imageInput" type="file" />
<input type="submit"  id="submit-btn" value="Upload" />
<img src="images/ajax-loader.gif" id="loading-img" style="display:none;" alt="Please Wait"/>
</form>
<div id="progressbox" style="display:none;"><div id="progressbar"></div ><div id="statustxt">0%</div></div>
<div id="output"></div>
</div>
</div>

CSS

#progressbox {
border: 1px solid #0099CC;
padding: 1px; 
position:relative;
width:400px;
border-radius: 3px;
margin: 10px;
display:none;
text-align:left;
}
#progressbar {
height:20px;
border-radius: 3px;
background-color: #003333;
width:1%;
}
#statustxt {
top:3px;
left:50%;
position:absolute;
display:inline-block;
color: #000000;
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
jth_92
  • 1,120
  • 9
  • 23
0

Have a look at this answer on stackoverflow. This does work and the link is not gonna die any time soon.

Here is the javascript code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="a1" data-filename="filename.xml">Click to download</div>
<script>
$('#a1').click(function() {
    var that = this;
    var page_url = 'download.php';

    var req = new XMLHttpRequest();
    req.open("POST", page_url, true);
    req.addEventListener("progress", function (evt) {
        if(evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            console.log(percentComplete);
        }
    }, false);

    req.responseType = "blob";
    req.onreadystatechange = function () {
        if (req.readyState === 4 && req.status === 200) {
            var filename = $(that).data('filename');
            if (typeof window.chrome !== 'undefined') {
                // Chrome version
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(req.response);
                link.download = filename;
                link.click();
            } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
                // IE version
                var blob = new Blob([req.response], { type: 'application/force-download' });
                window.navigator.msSaveBlob(blob, filename);
            } else {
                // Firefox version
                var file = new File([req.response], filename, { type: 'application/force-download' });
                window.open(URL.createObjectURL(file));
            }
        }
    };
    req.send();
});
</script>
Community
  • 1
  • 1
Ayan
  • 2,738
  • 3
  • 35
  • 76