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();
  }

 

posted @ 2021-07-06 14:41  面包_girl  阅读(3112)  评论(0编辑  收藏  举报
/* 鼠标点击文字特效 */