获取文件hash有什么用呢?当然是用来实现分片上传和断点续传和秒传功能的,这里要用到spark-md5
,没有的话先安装下
import SparkMD5 from 'spark-md5';
getFileHash(file){
return new Promise( resolve => {
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = ev => {
let buffer = ev.target.result,
spark = new SparkMD5.ArrayBuffer(),
HASH,
suffix;
spark.append(buffer);
HASH = spark.end();
suffix = file.name.substring(file.name.lastIndexOf(".") + 1);
resolve({
buffer,
HASH,
suffix,
filename: `${HASH}.${suffix}`
});
};
});
},
//获取文件hash值以及后缀名
let {
HASH,
suffix
} = await this.getFileHash(file);