, , ( , 100% ) (. "" ):
, , % -Value, script . , , PHP , .
, :
download.php, . javascript, , (.. download.php?fileId=5&chunk=59). Javascript , , .
, Javascript , : , " ". , , .
- , ( - ), . JavaScript, ( ) , php/Javascript. ( Javascript, , , .)
, :
, , :
, / .
a download.php, . ChunkSize "10", ( → )
<?
header('Cache-Control: max-age=31556926');
$etag = 'a_unique_version_string';
header('ETag: "'.$etag.'"');
$chunkCount = 10;
$file = $_GET["file"];
$file = "someImage.jpg";
$chunk = $_GET["chunk"];
$fileSize = filesize($file);
$chunkSize = ceil($fileSize / $chunkCount);
$handle = fopen($file, "r");
$start = ($chunk-1) * $chunkSize + ($chunk-1);
$toRead = min($chunkSize+1, $fileSize - $start);
$end = $start + $toRead;
if (fseek($handle, $start) == 0){
$c = fread($handle, $toRead);
echo $c;
@fclose($handle);
}else{
}
?>
, URL- ( ) :
downloading http://dog-net.org/dltest/download.php?file=1&chunk=1
downloading http://dog-net.org/dltest/download.php?file=1&chunk=2
downloading http://dog-net.org/dltest/download.php?file=1&chunk=3
downloading http://dog-net.org/dltest/download.php?file=1&chunk=4
downloading http://dog-net.org/dltest/download.php?file=1&chunk=5
, JavaScript .
. " ". , . : , , ,
<html>
<head>
<script language="javascript">
var urls = new Array();
urls[0] = "http://dog-net.org/dltest/download.php?file=1&chunk=1";
urls[1] = "http://dog-net.org/dltest/download.php?file=1&chunk=2";
urls[2] = "http://dog-net.org/dltest/download.php?file=1&chunk=3";
urls[3] = "http://dog-net.org/dltest/download.php?file=1&chunk=4";
urls[4] = "http://dog-net.org/dltest/download.php?file=1&chunk=5";
urls[5] = "http://dog-net.org/dltest/download.php?file=1&chunk=6";
urls[6] = "http://dog-net.org/dltest/download.php?file=1&chunk=7";
urls[7] = "http://dog-net.org/dltest/download.php?file=1&chunk=8";
urls[8] = "http://dog-net.org/dltest/download.php?file=1&chunk=9";
urls[9] = "http://dog-net.org/dltest/download.php?file=1&chunk=10";
var fileContent = new Array();
function downloadChunk(chunk){
var url = urls[chunk-1];
console.log("downloading " + url);
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = 'blob';
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
document.getElementById("log").innerHTML += "downloading " + url + "<br />";
fileContent.push(xhr.response);
document.getElementById("percentage").innerHTML = chunk / urls.length * 100;
if (chunk < urls.length){
downloadChunk(chunk+1);
}else{
finishFile();
}
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);
}
function finishFile(){
contentType = 'image/jpg';
console.log("Generating file");
var a = document.createElement('a');
var blob = new Blob(fileContent, {'type':contentType, 'endings':'native'});
console.log("File generated. size: " + blob.size);
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1){
var url = window.URL.createObjectURL(blob);
window.location = url;
}
if (!(window.ActiveXObject) && "ActiveXObject"){
if (window.chrome){
a.href = window.URL.createObjectURL(blob);
a.download = "download";
a.click();
}else{
window.navigator.msSaveOrOpenBlob(blob, 'download');
}
}
}
function setProgress(chunk){
document.getElementById("percentage").innerHTML = chunk / urls.length * 100;
}
</script>
</head>
<body onload="downloadChunk(1);">
<div id="percentage"></div>
<div id="log"></div>
</body>
</html>
, Blobs - ... IE 11, Chrome 32 Firefox 27. Safari. .
: http://dog-net.org/dltest/ ( png-, paint/irfranview/whatevs - .)
. Second Call , , ( ) . ( "Forever" - , 7 !)
:
- JavaScript ( )
- finalFile- .
- . ( 30 ).
- Mime-Type .
- .
- , .
, , .
/Java/Silverlight, , !