Spark-MD5获取文件MD5的使用

function calculate(file,callBack){  
    var fileReader = new FileReader(),  
        blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice || File.prototype.slice,  
        chunkSize = 2097152,  
        // read in chunks of 2MB  
        chunks = Math.ceil(file.size / chunkSize),  
        currentChunk = 0,  
        spark = new SparkMD5();  
  
    fileReader.onload = function(e) {  
        spark.appendBinary(e.target.result); // append binary string  
        currentChunk++;  
  
        if (currentChunk < chunks) {  
            loadNext();  
        }  
        else {  
            callBack(spark.end());
        }  
    };  
  
    function loadNext() {  
        var start = currentChunk * chunkSize,  
            end = start + chunkSize >= file.size ? file.size : start + chunkSize;  
  
        fileReader.readAsBinaryString(blobSlice.call(file, start, end));  
    };  
  
    loadNext();  
}  

参数:file 选取的文件

callBack 回调函数可以返回获取的MD5

callBack=function(md5){

//处理信息

}

你可能感兴趣的:(JavaScript)