1.安装spark MD5
npm i spark MD5
2.vue文件中引入
import SparkMD5 from ''spark-md5''
3.使用
- 方法一:
- handlePrepareUpload() {
- var fileReader = new FileReader();
- var dataFile = this.fileList[0];
- const _this = this
- var spark = new SparkMD5(); //创建md5对象(基于SparkMD5)
- if (dataFile.size > 1024 * 1024*10) {
- var data1 = dataFile.slice(0, 1024 * 1024*10); //将文件进行分块 file.slice(start,length)
- fileReader.readAsBinaryString(data1); //将文件读取为二进制码
- } else {
- fileReader.readAsBinaryString(dataFile);
- }
-
- //文件读取完毕之后的处理
- //a639e28526d1809745b46bf1189594fe 6d9efe0c593b1383482feb229318e03a
- fileReader.onload = function(e) {
- spark.appendBinary(e.target.result);
- var md5 = spark.end()
- console.log(md5)
- _this.getMd5Checked(md5)
- };
- },
- 方法二:经测试可用
- handlePrepareUpload() {
- var file = this.fileList[0]
- const fileSize = file.size; // 文件大小
- const chunkSize = 1024 * 1024 * 10; // 切片的大小
- const chunks = Math.ceil(fileSize / chunkSize); // 获取切片个数
- const fileReader = new FileReader();
- const spark = new SparkMD5.ArrayBuffer();
- const bolbSlice =
- File.prototype.slice ||
- File.prototype.mozSlice ||
- File.prototype.webkitSlice;
- let currentChunk = 0;
-
- fileReader.onload = e => {
- const res = e.target.result;
- spark.append(res);
- currentChunk++;
- if (currentChunk < chunks) {
- loadNext();
- console.log(`第${currentChunk}分片解析完成, 开始第${currentChunk +1}分片解析`);
- } else {
- const md5 = spark.end();
- console.log(''解析完成'');
- alert(md5)
- }
- };
-
- const loadNext = () => {
- const start = currentChunk * chunkSize;
- const end =
- start + chunkSize > file.size ? file.size : start + chunkSize;
- fileReader.readAsArrayBuffer(bolbSlice.call(file, start, end));
- };
- loadNext();
- },
作者:二营长家的张大炮
链接:https://www.jianshu.com/p/59d076ff88e0
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。