spark-MD5文件MD5加密

npm地址:https://www.npmjs.com/package/spark-md5
  //生成MD5
  md5Count() {
    let blobSlice = File.prototype.slice,
      file = this.importFormData.File,  // file
      chunkSize = 2097152,                             // Read in chunks of 2MB
      chunks = Math.ceil(file.size / chunkSize),
      currentChunk = 0,
      spark = new SparkMD5(),
      fileReader = new FileReader();

    fileReader.onload = (e) => {
      spark.appendBinary(e.target.result);                   // Append a binary string
      currentChunk++;

      if (currentChunk < chunks) {
        loadNext();
      } else {
        this.importFormData.SignFileMd5 = spark.end();
        console.log(this.importFormData.SignFileMd5);
      }
    };

    fileReader.onerror = function () {
      console.warn('oops, something went wrong.');
    };

    function loadNext() {
      let start = currentChunk * chunkSize,
        end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;

      fileReader.readAsBinaryString(blobSlice.call(file, start, end));
    }

    loadNext();
  }

调用,在input  change时,调用此函数

  uploadUser() {
    // @ts-ignore
    const files = document.getElementById('uploadUserFile').files;
    this.importFormData.File = files[0];
    this.md5Count();
  }

原文地址:https://www.cnblogs.com/huangmin1992/p/14976882.html