JDK 配置参数
- 调用方法
uploadHandler
的时候,需要传递参数的注意事项。 - 可以通过调用方法
uploadHandler.config(config: UploadConfigType)
来传递参数
配置总览
/* 上传文件的 配置文件 */
export type UploadConfigType = Partial<{
// 最大重试次数
maxRetryTimes: number;
// 并发限制次数
concurrentLimit: number;
// 表示 基础网速
baseNetworkSpeed: number;
// 是否持久化
persist: boolean;
// 表示语言类型
language: LanguageEnumType;
// 最大缓存 hash 数
maxHashNameCount: number;
}> & {
// 请求接口
req: {
listFilesReq: IListFilesReq | null;
sectionUploadReq: ISectionUploadReq | null;
mergeUploadReq: IMergeUploadReq | null;
verifyFileExistReq: IVerifyFileExistReq | null;
};
};
字段 | 含义 | 类型 | 默认 | 是否必须 |
---|---|---|---|---|
maxRetryTimes | 失败后最大重试次数 | number | 3 | No |
concurrentLimit | 最大并发限制 | number | 2 | No |
baseNetworkSpeed | 表示基础网速(这个基础上提升网速) | number | 1024 | No (默认是kb) |
persist | 是否支持持久化 | boolean | false | No |
maxHashNameCount | 缓存名称的最大数 | number | ※1 | No |
language | 国际化语言 | LanguageEnumType | ※6 | No |
req.listFilesReq | ※2 | ※2 | - | yes |
req.sectionUploadReq | ※3 | ※3 | - | yes |
req.mergeUploadReq | ※4 | ※4 | - | yes |
req.verifyFileExistReq | ※5 | ※5 | - | yes |
※1
为了提高上传速度,每个文件计算出的Hash值都会通过
indexedDB
将其持久化,但是可以设置最大值,默认值是200
, 如果不想要此功能的话,可以设置<= 0
的值
※2
// 表示 请求响应类型
export type ICommonResponse<T = unknown> = {
data: T;
message?: string;
msg?: string;
success?: boolean;
code: number;
};
这个是接口需要返回的基础类型。 但是此类型有几个须知的知识点
- 如何判断请求是否正确???
- 如果存在属性
success
的话,一定要满足code === "200" && success == true
, 反之满足条件code === "200"
即可 - 当请求错误时,如果返回值中存在
msg
ormessage
的话,会将消息体现到进度消息中。如果该属性不存在也不会有任何影响
- 如果存在属性
// 表示接口类型
export type IListFilesReq = (
calculationHashCode: string
) => Promise<ICommonResponse<[number, number]>>;
为了实现 断点续传 的功能,需要该接口提供已经上传的文件列表
- 参数
calculationHashCode
根据文件内容生成的 Hash 值,如下面的76a591f88bcecd8076ec07c7a402fc39566aeb4b5f5020dd045d2a67cc45debe
实际的案例
请求接口 GET /upload/list/76a591f88bcecd8076ec07c7a402fc39566aeb4b5f5020dd045d2a67cc45debe
相应值
- data
[2, 10000]
[上传文件个数, 上传文件字节个数]
{
"success": true,
"data": [
2,
10000
],
"code": "200",
"message": null
}
※3
// 表示 请求响应类型
export type ISectionUploadReq = (
calculationHashCode: string,
chunkFileName: string,
formData: FormData
) => Promise<ICommonResponse<boolean>>;
- 参数
calculationHashCode
根据文件内容计算的 hash 值 - 参数
chunkFileName
要上传文件的文件名称 - 参数
formData
表示上传文件的文件,FormData
的 key 是file
(这个是作为 post 的 body 发出的)
实现 分片上传 的接口
实际的案例
请求接口 POST /upload/section/76a591f88bcecd8076ec07c7a402fc39566aeb4b5f5020dd045d2a67cc45debe/76a591f88bcecd8076ec07c7a402fc39566aeb4b5f5020dd045d2a67cc45debe.zip-7
响应值
{
"success": true,
"data": true,
"code": "200",
"message": null
}
※4
为了完成 切片文件合并的接口
export type IMergeUploadReq = (
calculationHashCode: string,
fileName: string
) => Promise<ICommonResponse>;
- 参数
calculationHashCode
根据文件内容计算的 hash 值 - 参数
fileName
要上传文件的文件名称
实际案例
请求接口 GET /upload/merge/c6a1ec6ea98fe06b8568af968a68a354b3491cd5b69adbce4eac2dedfff0a6d1/c6a1ec6ea98fe06b8568af968a68a354b3491cd5b69adbce4eac2dedfff0a6d1.mp3
响应值
{
"success": true,
"data": true,
"code": "200",
"message": null
}
※5
为了实现 秒传 功能,提供了查询文件是否已经存在接口
export type IVerifyFileExistReq = (
calculationHashName: string
) => Promise<ICommonResponse<boolean>>;
- 参数
calculationHashCode
根据文件内容计算的 hash 值
实际案例
请求接口 /upload/verify/c6a1ec6ea98fe06b8568af968a68a354b3491cd5b69adbce4eac2dedfff0a6d1.mp3
相应值
{
"success": true,
"data": false,
"code": "200",
"message": null
}
※6
本插件目前支持了国际化。 默认值是:
LanguageEnumType.ZH
, 有多种方式可以修改国际化语言的方式
方法1
// 默认是 汉语 LanguageEnumType.ZH
uploadHandler.lng = async function (language?: LanguageEnumType) {
// todo
};
方法2
// 参数类型【UploadConfigType】,包含了语言类型
uploadHandler.config = function (config: UploadConfigType) {
// todo
};
JDK 回调
type ProgressReturnType = [baseDir: string, fileName: string];
declare function uploadHandler(
uploadFile: File,
callback?: (arr: ProgressReturnType) => void
): Promise<ProgressReturnType>;
方法
uploadHandler
不仅支持 参数回调方法,同时支持 Promise,都会返回一个类型为ProgressReturnType
的数组
[baseDir: string, fileName: string]
=== [上传文件名称, 原文件名称](包含后缀)