Plupload callback get file md5

173 views Asked by At

Plupload plugin callback get uploaded file md5.

Plupload has the same question, but others can't get the right answer.

Below code is my test. please confirm.

  • import spark-md5
<!-- spark-md5 -->
<script src="https://cdn.bootcdn.net/ajax/libs/spark-md5/3.0.0/spark-md5.js"></script>
  • calc md5 method
// file md5 method
function calculate(file, callBack) {
    let 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() {
        let start = currentChunk * chunkSize,
            end = start + chunkSize >= file.size ? file.size : start + chunkSize;
        fileReader.readAsBinaryString(blobSlice.call(file, start, end));
    };
    loadNext();
}
  • file upload callback get file md5
// 文件上传回调
function uploadCallback(status, remote_path, filename, callback_params, file) {
    // 调用计算md5的方法
    calculate(file.getNative(), function(md5) {
        console.log(md5); //hash值
    });
    // other code ....
    // ....
});

Plupload https://www.phpin.net/tools/plupload/

1

There are 1 answers

0
meadhu On

I used this web site calc file md5 compare with up code. It's right.

HTML5 File Hash Online Calculator