class ChunkUploader<TMetadata extends Metadata> {
constructor(options: ChunkUploaderOptions<TMetadata>);
get status(): ChunkUploaderStatus;
get bytesUploaded(): number;
get error(): unknown;
get file(): File;
start(): boolean;
get canStart(): boolean;
pause(): boolean;
get canPause(): boolean;
resume(): boolean;
get canResume(): boolean;
abort(): boolean;
get canAbort(): boolean;
}
new ChunkUploader(options: ChunkUploaderOptions)
- Parameters
- options: ChunkUploaderOptions
status: 'pending' | 'uploading' | 'pausing' | 'paused' | 'aborted' | 'complete' | 'error'
The status of the uploader
Reference: How status changes
bytesUploaded: number
Bytes uploaded so far.
error: unknown
The error that occurred during the upload process. This is undefined
if no error occurred.
file: File
The file for the upload process.
start(): boolean
Start the upload process. returns false
if the status is not pending
.
- status:
pending
->uploading
->complete
orerror
canStart: boolean
true
if the status is pending
.
pause(): boolean
Pause the upload process. returns false
if the status is not uploading
.
Note that the status at the end could be complete
if the last chunk is being uploaded when the function is called.
- status:
uploading
->pausing
->paused
orcomplete
canPause: boolean
true
if the status is uploading
.
resume(): boolean
Resume the upload process. returns false
if the status is not paused
or error
.
- status:
paused
orerror
->uploading
->complete
orerror
canResume: boolean
true
if the status is paused
or error
.
abort(): boolean
Abort the upload process. returns false
if the status is not paused
or error
.
- status:
paused
orerror
->aborted
canAbort: boolean
true
if the status is paused
or error
.
interface ChunkUploaderOptions<TMetadata extends Metadata> {
file: File;
onChunkUpload: ChunkUploadHandler<TMetadata>;
metadata: TMetadata;
chunkBytes?: number;
retryDelays?: number[];
onChunkComplete?: (bytesAccepted: number, bytesTotal: number) => void;
onSuccess?: () => void;
onError?: (error: unknown) => void;
onPaused?: () => void;
onAborted?: (metadata: TMetadata) => void;
onStatusChange?: (
oldStatus: ChunkUploaderStatus | undefined,
newStatus: ChunkUploaderStatus
) => void;
}
file: File
The file to be uploaded
onChunkUpload: (chunkFormData, metadata) => Promise<void>
The function that defines how the chunk is uploaded to the server.
- Parameters
- chunkFormData: ChunkFormData
- metadata: Metadata
metadata: (TMetadata extends Record<string, string | boolean | number | undefined | null>)
The metadata to send with each chunk. This can be used to send additional information like the file name, file type, etc.
Default: 5MB
(5 * 1024 * 1024)
chunkBytes?: number
The number of bytes to send in each chunk.
Default: [1000, 2000, 4000, 8000]
retryDelays?: number[]
Milliseconds to wait before retrying a failed chunk upload. Set to an empty array to disable retries.
Default: undefined
onChunkComplete?: (bytesAccepted: number, bytesTotal: number) => void
A callback that is called when a chunk is uploaded.
Default: undefined
onSuccess?: () => void
A callback that is called when the file is sucessfully uploaded.
Default: undefined
onError?: (error: unknown) => void
A callback that is called when an error occurs during the upload process.
Default: undefined
onPaused?: () => void
A callback that is called when the upload process is paused.
Default: undefined
onAborted?: (metadata) => void
A callback that is called when the upload process is aborted.
- Parameters
- metadata: Metadata
Default: undefined
onStatusChange?: (oldStatus, newStatus) => void
A callback that is called when the status of the uploader changes.
- Parameters
- oldStatus: ChunkUploaderStatus |
undefined
- newStatus: ChunkUploaderStatus
- oldStatus: ChunkUploaderStatus |
interface ChunkFormData {
get(name: 'blob'): Blob;
get(name: 'offset'): `${number}`;
get(name: 'length'): `${number}`;
get(name: 'retry'): `${number}`;
get(name: 'total'): `${number}`;
get(name: 'isLastChunk'): 'true' | 'false';
}
The form data that is sent with each chunk. This includes the chunk blob, the offset, the length, and other information.