UNPKG

xframelib

Version:

积累的前端开发基础库,来源于项目和服务于项目。

104 lines (103 loc) 2.54 kB
import { XhrHeaders } from 'xhr'; export type FileEventName = 'attempt' | 'attemptFailure' | 'chunkSuccess' | 'error' | 'offline' | 'online' | 'progress' | 'success'; export type AllowedMethods = 'PUT' | 'POST' | 'PATCH'; /** * 文件分片传输返回结果 */ export interface ChunkResult { DATASOURCEID?: string; NAME: string; INDEX: number; STATE: boolean; END: boolean; FILEPATH?: string; } /** * 文件上传配置 */ export interface FileUploadOptions { endpoint: string | ((file?: File) => Promise<string>); file: File; method?: AllowedMethods; headers?: XhrHeaders; maxFileSize?: number; chunkSize?: number; attempts?: number; threadNumber?: number; /** * 单位:秒 */ delayBeforeAttempt?: number; md5?: string; } /** * 文件上传类 */ export declare class FileUpload { endpoint: string | ((file?: File) => Promise<string>); file: File; headers: XhrHeaders; method: AllowedMethods; chunkSize: number; attempts: number; delayBeforeAttempt: number; md5: string; private chunkCount; private chunkByteSize; private cacheSize; private currentMaxIndex; private maxFileBytes; private endpointValue?; private totalChunks; private attemptCount; private offline; private paused; private success; private isResumed; private mapXhrRequest; private okRecordList; private eventTarget; private fileName; constructor(options: FileUploadOptions); /** * 订阅事件 */ on(FileEventName: FileEventName, fn: (event: any) => void): void; abort(): void; pause(): void; resume(): Promise<void>; /** * 发送消息 */ private dispatch; /** *验证参数的正确性 */ private validateOptions; /** * Endpoint can either be a URL or a function that returns a promise that resolves to a string. */ private getEndpoint; /** * Get portion of the file of x bytes corresponding to chunkSize */ private getChunk; private xhrPromise; /** * Send chunk of the file with appropriate headers and add post parameters if it's last chunk */ private sendChunk; /** * 失败处理和尝试 */ /** * 进行分片上传,并处理错误和重试 */ private sendChunks; } /** * 创建文件上传对象方法 * @param options * @returns */ export declare const createFileUpload: (options: FileUploadOptions) => FileUpload;