checker
此为异步函数函数,用于生成文件hash值,用于断点续传、秒传等功能
使用方式
js
import { checker } from 'enlarge-file-upload';
const file = document.querySelector('input[type="file"]').files[0];
const result = await checker(file, {
... 你的配置
});参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| file | File | 是 | 文件对象 |
| config | CheckerConfig | 是 | 配置项 |
返回值
| 参数名 | 类型 | 说明 |
|---|---|---|
| result | CheckerResult | 返回值 |
ts类型
ts
export interface CheckerResult {
/**
* 所有切片hashMap
*/
chunkHashMap: Map<number, string>;
/**
* 文件哈希值
*/
hash: string;
/**
* 总分片数
*/
totalChunks: number;
/**
* 抽样hash数据
*/
sampleHash: {
/**
* 抽样hash值
*/
sampleHash: string,
/**
* key为抽样hash索引,value为单片抽样hash值
*/
hashObj: Record<number,string>,
};
/**
* 所有切片
*/
allChunks: Blob[];
}
interface CheckerConfig
extends Omit<
Config,
| "startOffset"
| "concurrency"
| "maxRetries"
| "includeChunks"
| "onProgress"
| "onSpeed"
| "onSuccess"
| "uploadFunction"
> {
/**
* 【可选】是否开启抽样hash计算,默认 false
* @default false
*/
sampleHash?: boolean | ((totalChunks: number) => number[]);
}
declare function checker(
file: File,
config: CheckerConfig
): Promise<CheckerResult>;CheckerConfig配置
| 参数名 | 类型 | 必填 | 默认值 | 说明 | 支持版本 |
|---|---|---|---|---|---|
chunkSize | Number | 否 | 5 * 1024 * 1024 | 单个切片大小(单位:字节),默认5MB | - |
hash | Boolean | 否 | true | 是否计算文件整体哈希值(用于秒传验证) | - |
threads | number | 否 | 系统CPU核心数 - 2 | 计算hash所需要的webworker线程数 | - |
customHashApihashMode | 'crypto' | 'sha256' | 否 | 'crypto' | 自定义哈希计算 API,默认使用 Web Crypto API,也可以选择使用 sha256 算法 | - |
awaitHash | Boolean | 否 | true | 是否等待哈希计算完成(仅当hash:true时生效),大文件建议设为false | - |
sampleHash | Boolean | Function | 否 | false | 开启抽样hash计算:Boolean-使用库自带的索引逻辑Function-自定义抽样的分片索引 | 2.3.8 |
chunkMap | Boolean | Object | 否 | false | 分片哈希计算配置:true-同步计算所有分片hash{indices:[], async:true}-async为true开启异步计算,indices为需要计算的分片索引数组,默认计算所有分片 | - |
beginHash | Function | 否 | - | 开始计算哈希时的回调(仅当hash:true时触发) | - |
endHash | Function | 否 | - | 哈希计算完成时的回调(仅当hash:true时触发) | - |
CheckerResult属性
| 参数名 | 类型 | 说明 |
|---|---|---|
chunkHashMap | Map<number, string> | 所有切片hashMap |
hash | string | 文件哈希值 |
totalChunks | number | 总分片数 |
allChunks | Blob[] | 所有切片 |
sampleHash | Object | 抽样hash值及相关数据 |