瀏覽器獲取檔案MD5值
阿新 • • 發佈:2018-11-25
能做到大檔案md5值的獲取,但是檔案越大耗時越久。
spark-md5工具包:https://download.csdn.net/download/weixin_36524613/10545957
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="../../static/js/spark-md5.min.js"></script> </head> <body> <form method="POST" enctype="multipart/form-data"> <input id=file type=file /> </form> <script> document.getElementById("file").addEventListener("change", function() { var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice, file = this.files[0], chunkSize = 10485760,//每次讀10M chunks = Math.ceil(file.size / chunkSize), currentChunk = 0, spark = new SparkMD5.ArrayBuffer(), frOnload = function(e){ spark.append(e.target.result); // append array buffer currentChunk++; if (currentChunk < chunks) loadNext(); else alert(spark.end()); }, frOnerror = function () { alert("error!") }; function loadNext() { var fileReader = new FileReader(); fileReader.onload = frOnload; fileReader.onerror = frOnerror; var start = currentChunk * chunkSize, end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize; fileReader.readAsArrayBuffer(blobSlice.call(file, start, end)); }; loadNext(); }); </script> </body> </html>