javascript 分片下载

#html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://newpass.com/static/vendor/angular/angular.min.js"></script>
    <script src="http://newpass.com/test/md51/md5.js"></script>
    <script src="//cdn.rawgit.com/satazor/SparkMD5/master/spark-md5.min.js"></script>

</head>
<body>
<div ng-app="testapp" ng-controller="testctl">
    <input type="button" ng-click="one()" value="one">
</div>

<script>
    (function () {
        'use strict';
        window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
        angular
                .module('testapp', [
                ])
                .controller('testctl',['$scope','$timeout','$http','$q',function ($scope,$timeout,$http,$q) {
                    $scope.isdel=false;

                    $scope.md5 = function (file) {
                        var deferral_local = $q.defer();
                        var defer = $q.defer();
                        var promise = defer.promise;

                        var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
                        var persize = 2097152; // read in chunks of 2MB
                        var allpage = Math.ceil(file.size / persize);
                        var page = 0;
                        var spark = new SparkMD5.ArrayBuffer();

                        function loadNext() {
                            var fileReader = new FileReader();
                            fileReader.onload = function (e) {
                                spark.append(e.target.result); // append array buffer
                                page = page+1;
                                if (page < allpage)
                                    loadNext();
                                else
                                    defer.resolve(spark.end());
                            };
                            var start = page * persize;
                            var end = ((start + persize) >= file.size) ? file.size : start + persize;
                            fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
                        }

                        promise.then(loadNext())
                                .then(function (md5) {
                                    deferral_local.resolve( { status: md5 } );
                                });

                        return deferral_local.promise;
                    };


                    $scope.listfile = function () {
                        window.requestFileSystem(window.TEMPORARY, 5*1024*1024*1024, function(fs) {
                            fs.root.getFile('aw.txt', {create: false}, function (fileEntry) {
                                fileEntry.file(function (file) {
                                    $q.when($scope.md5(file)).then(function (md5) {
                                        console.log(md5);
                                        var link = document.createElement("a");
                                        link.setAttribute("href", fileEntry.toURL());
                                        link.setAttribute("download", "xxx.txt");
                                        link.style.visibility = 'hidden';
                                        document.body.appendChild(link);
                                        link.click();
                                        document.body.removeChild(link);
                                    });
                                });
                            });
                            });
                    };

                    $scope.one = function () {
                        $scope.defer = $q.defer();
                        $scope.promise = $scope.defer.promise;
                        $scope.promise.then($scope.three()).then(function () {
                            $scope.listfile();
                        });

                    };


                    $scope.downloadfile = function (page){
                        page = page || 1;

                        if(page === 1 && !$scope.isdel){
                            window.requestFileSystem(window.TEMPORARY, 5*1024*1024*1024, function(fs) {
                                fs.root.getFile('aw.txt', {create: false}, function (fileEntry) {
                                    if (fileEntry.isFile && !$scope.isdel) {
                                        fileEntry.remove(function () {
                                            console.log('File successufully removed.');
                                            $scope.isdel=true;
                                            $scope.three();
                                        }, function(){
                                            console.log(1111);
                                        });
                                    }else{
                                        $scope.isdel=true;
                                        $scope.three();
                                    }
                                },function(e){
                                    $scope.isdel=true;
                                    $scope.three();
                                    console.log(2222);
                                    console.log(e);
                                });
                            },function(){
                                console.log(3333);
                            });
                        }else{
                            $http.get("/test/down1/5.php?page="+page).success(function (response){
                                page = page+1;
                                window.requestFileSystem(window.TEMPORARY, 5*1024*1024*1024, function(fs){
                                    fs.root.getFile('aw.txt', {create: true}, function(fileEntry) {
                                        fileEntry.createWriter(function(fileWriter) {
                                            window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;
                                            fileWriter.seek(fileWriter.length);

                                            $scope.byteCharacters = atob(response.content);
                                            $scope.bytesLength = $scope.byteCharacters.length;
                                            $scope.bytes = new Array($scope.bytesLength);
                                            for(var i=0; i<$scope.bytesLength;i++){
                                                $scope.bytes[i] = $scope.byteCharacters[i].charCodeAt(0);
                                            }
                                            var content_tmp=new Uint8Array($scope.bytes);

                                            var blob = new Blob([content_tmp]);
                                            fileWriter.write(blob);
                                            console.log("gggggg");
                                            fileWriter.onwriteend = function (e) {
                                                if(page>response.allpage){
                                                    $scope.isdel=false;
                                                    $scope.defer.resolve(fileEntry);
                                                }else{
                                                    $scope.downloadfile(page);
                                                }
                                            }
                                        }, function(e){
                                            console.log(44444);
                                            console.log(e);
                                        });
                                    }, function(){
                                        console.log(555555);
                                    });
                                }, function(){
                                    console.log(66666);
                                });
                            });

                        }
                    };

                }]);

    }());

</script>

</body>
</html>

#php

$page=isset($_GET['page'])?$_GET['page']:1;
$perpage=30;
$filename="t.txt";
$size=filesize($filename);
$file = fopen($filename,"r");
$allpage = ceil($size/$perpage);
$start = ($page-1)*$perpage;
fseek($file,$start,SEEK_SET);
$content = fread($file,$perpage);

$result['allpage']=$allpage;
$result['page']=$page;
$result['content']=$content;

echo json_encode($result);

发表评论

电子邮件地址不会被公开。 必填项已用*标注