Spark-MD5获取文件MD5的hash标识

原文链接:https://blog.csdn.net/anan890624/article/details/52296162

先自己下载 Spark-MD5
npm install spark-md5

function calculate(file,callBack){  
		/*
		* 	file 选取的文件
		* 	callBack 回调函数可以返回获取的MD5
		*/
    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();  
};

举个栗子:




	speak-MD5



	








你可能感兴趣的:(Spark-MD5获取文件MD5的hash标识)