Skip to content

JDK 配置参数

  • 调用方法 uploadHandler 的时候,需要传递参数的注意事项。
  • 可以通过调用方法uploadHandler.config(config: UploadConfigType) 来传递参数

配置总览

typescript
/* 上传文件的 配置文件 */
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失败后最大重试次数number3No
concurrentLimit最大并发限制number2No
baseNetworkSpeed表示基础网速(这个基础上提升网速)number1024No (默认是kb)
persist是否支持持久化booleanfalseNo
maxHashNameCount缓存名称的最大数number※1No
language国际化语言LanguageEnumType※6No
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

typescript
// 表示 请求响应类型
export type ICommonResponse<T = unknown> = {
    data: T;
    message?: string;
    msg?: string;
    success?: boolean;
    code: number;
};

这个是接口需要返回的基础类型。 但是此类型有几个须知的知识点

  • 如何判断请求是否正确???
    • 如果存在属性success的话,一定要满足code === "200" && success == true, 反之满足条件code === "200"即可
    • 当请求错误时,如果返回值中存在 msg or message 的话,会将消息体现到进度消息中。如果该属性不存在也不会有任何影响
typescript
// 表示接口类型
export type IListFilesReq = (
    calculationHashCode: string
) => Promise<ICommonResponse<[number, number]>>;

为了实现 断点续传 的功能,需要该接口提供已经上传的文件列表

  • 参数 calculationHashCode 根据文件内容生成的 Hash 值,如下面的76a591f88bcecd8076ec07c7a402fc39566aeb4b5f5020dd045d2a67cc45debe

实际的案例

请求接口 GET /upload/list/76a591f88bcecd8076ec07c7a402fc39566aeb4b5f5020dd045d2a67cc45debe 相应值

  • data
    • [2, 10000] [上传文件个数, 上传文件字节个数]
json
{
  "success": true,
  "data": [
    2,
    10000
  ],
  "code": "200",
  "message": null
}

※3

typescript
// 表示 请求响应类型
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

响应值

json
{
  "success": true,
  "data": true,
  "code": "200",
  "message": null
}

※4

为了完成 切片文件合并的接口

typescript
export type IMergeUploadReq = (
    calculationHashCode: string,
    fileName: string
) => Promise<ICommonResponse>;
  • 参数calculationHashCode 根据文件内容计算的 hash 值
  • 参数fileName 要上传文件的文件名称

实际案例

请求接口 GET /upload/merge/c6a1ec6ea98fe06b8568af968a68a354b3491cd5b69adbce4eac2dedfff0a6d1/c6a1ec6ea98fe06b8568af968a68a354b3491cd5b69adbce4eac2dedfff0a6d1.mp3

响应值

json
{
  "success": true,
  "data": true,
  "code": "200",
  "message": null
}

※5

为了实现 秒传 功能,提供了查询文件是否已经存在接口

typescript
export type IVerifyFileExistReq = (
    calculationHashName: string
) => Promise<ICommonResponse<boolean>>;
  • 参数calculationHashCode 根据文件内容计算的 hash 值

实际案例

请求接口 /upload/verify/c6a1ec6ea98fe06b8568af968a68a354b3491cd5b69adbce4eac2dedfff0a6d1.mp3 相应值

json
{
  "success": true,
  "data": false,
  "code": "200",
  "message": null
}

※6

本插件目前支持了国际化。 默认值是:LanguageEnumType.ZH, 有多种方式可以修改国际化语言的方式

方法1

typescript
// 默认是 汉语 LanguageEnumType.ZH 
uploadHandler.lng = async function (language?: LanguageEnumType) {
    // todo
};

方法2

typescript
// 参数类型【UploadConfigType】,包含了语言类型 
uploadHandler.config = function (config: UploadConfigType) {
    // todo
};

JDK 回调

typescript
type ProgressReturnType = [baseDir: string, fileName: string];

declare function uploadHandler(
    uploadFile: File,
    callback?: (arr: ProgressReturnType) => void
): Promise<ProgressReturnType>;

方法uploadHandler 不仅支持 参数回调方法,同时支持 Promise,都会返回一个类型为ProgressReturnType的数组

[baseDir: string, fileName: string] === [上传文件名称, 原文件名称](包含后缀)