分片上传

<html>
<head>
    <title>test</title>
</head>
<body>
    <input type='file' onchange="uploadFile(this)">
</body>
<script type="text/javascript">
function uploadFile(data) {
    var file=data.files[0]
    var chunkSize = 1024 * 1024;   // 每片1M大小
    var totalSize = file.size;
    var chunkQuantity = Math.ceil(totalSize/chunkSize);  //分片总数
    var offset = 0;  // 偏移量

    var reader = new FileReader();
    reader.onload = function(e) {
        ++offset;
        if(offset === chunkQuantity) {
            console.log("上传完成")
        } else if(offset === chunkQuantity-1){
            blob = file.slice(offset*chunkSize, totalSize);   // 上传最后一片
            reader.readAsBinaryString(blob);
            console.log(blob)
        } else {
            blob = file.slice(offset*chunkSize, (offset+1)*chunkSize);   
            reader.readAsBinaryString(blob);
            console.log(Object.prototype.toString.call(blob))
            console.log(blob)
        }
    }
    var blob = file.slice(0, chunkSize);
    reader.readAsBinaryString(blob);
}
</script>
</html>

 

posted on 2018-09-16 02:29  carlyin  阅读(183)  评论(0)    收藏  举报

导航