/ SDKs / JavaScript
SDKs
Chat SDKs JavaScript v4
Chat SDKs JavaScript
Chat SDKs
JavaScript
Version 4

Cancel an in-progress file upload

Copy link

Using the cancelUploadingFileMessage() method, you can cancel an in-progress file upload while it hasn't been completed yet. If the function operates successfully, the value of true is returned.

Note: If you attempt to cancel the upload after it has already been completed or canceled, or the attempt results in an error, the function returns the value of false.

JavaScriptTypeScript
const params = {
    file: FILE,
    fileName: FILE_NAME,
    customType: CUSTOM_TYPE,
    data: DATA,
};
channel.sendFileMessage(params)
    .onPending((message) => {
        // ...
        channel.cancelUploadingFileMessage(message.reqId);
    });

If you upload a file using uploadFile(), You can cancel the upload by using the requestId, which is provided by the uploadStartedHandler callback included in the uploadFile() arguments.

let uploadingRequestId: string;
const {
    requestId,
    url,
} = await channel.uploadFile({
    file,
    uploadStartedHandler: (requestId: string) => {
        uploadingRequestId = requestId;
                ...
    },
});

...
// you can cancel the upload once uploadingRequestId is set in uploadStartedHandler
channel.cancelUploadingFileMessage(uploadingRequestId);