$(document).ready(function($)
{
$('.progress').asProgress
({
"namespace": "progress"
});
$("#DownloadIt").bind("click", DownloadIt);
});
function workerEventListener(event)
{
$('.progress').asProgress("go", event.data.download + "%");
$("#reportPlace").text(event.data.download + "%");
}
function DownloadIt()
{
var workerArgs =
{
"fileName": sourceFileURL,
"destinationFileName": destinationFileName
}
worker = new Worker("js/downloadThread.js");
worker.onmessage = workerEventListener;
worker.postMessage(workerArgs);
}
var fileName = "";
var fileSize = 0;
var chunkSize = 0;
var lastChunkSize = 0;
var xhr = new XMLHttpRequest();
var url = "curlDownload.php?";
var cmdGetFileSize = "getFileSize";
var cmdGetFileChunk = "downloadFileChunk";
var fileName = "";
var destinationFileName = "";
function getRequestFileSize()
{
requestURL = url + "cmd=" + cmdGetFileSize + "&fileName=" + fileName;
xhr.open("GET", requestURL, false);
xhr.onreadystatechange = function (e)
{
if (xhr.readyState === 4)
{
if(xhr.status == 200)
{
fileSize = parseInt(xhr.responseText);
}
}
}
xhr.send(null);
}
function getFileChunk(currentOffset, bufferSize)
{
requestURL = url +
"cmd=" + cmdGetFileChunk +
"&sourceFileName=" + fileName +
"&destinationFileName=" + destinationFileName +
"&offset=" + currentOffset +
"&size=" + (currentOffset + bufferSize - 1);
xhr.open("GET", requestURL, false);
xhr.send(null);
}
function downloadSourceFile()
{
bufferSize = chunkSize;
persentage = 0;
currentOffset = 0;
for(var counter = 0; counter < 100; counter++)
{
if(counter == 99)
{
bufferSize = lastChunkSize;
}
getFileChunk(currentOffset, bufferSize),
persentage++;
currentOffset += bufferSize;
var message =
{
"download": persentage
};
postMessage(message);
}
}
onmessage = function(event)
{
fileName = event.data.fileName;
destinationFileName = event.data.destinationFileName;
getRequestFileSize();
chunkSize = Math.ceil(fileSize / 100);
lastChunkSize = fileSize - (chunkSize * 99);
downloadSourceFile();
}