UploadTask interface

Represents the process of uploading an object. Allows you to monitor and manage the upload.

Signature:

exportinterfaceUploadTask

Properties

PropertyTypeDescription
snapshotUploadTaskSnapshotA snapshot of the current task state.

Methods

MethodDescription
cancel()Cancels a running task. Has no effect on a complete or failed task.
catch(onRejected)Equivalent to callingthen(null, onRejected).
on(event, nextOrObserver, error, complete)Listens for events on this task.Events have three callback functions (referred to asnext,error, andcomplete).If only the event is passed, a function that can be used to register the callbacks is returned. Otherwise, the callbacks are passed after the event.Callbacks can be passed either as three separate argumentsor as thenext,error, andcomplete properties of an object. Any of the three callbacks is optional, as long as at least one is specified. In addition, when you add your callbacks, you get a function back. You can call this function to unregister the associated callbacks.
pause()Pauses a currently running task. Has no effect on a paused or failed task.
resume()Resumes a paused task. Has no effect on a currently running or failed task.
then(onFulfilled, onRejected)This object behaves like a Promise, and resolves with its snapshot data when the upload completes.

UploadTask.snapshot

A snapshot of the current task state.

Signature:

snapshot:UploadTaskSnapshot;

UploadTask.cancel()

Cancels a running task. Has no effect on a complete or failed task.

Signature:

cancel():boolean;

Returns:

boolean

True if the cancel had an effect.

UploadTask.catch()

Equivalent to callingthen(null, onRejected).

Signature:

catch(onRejected:(error:StorageError)=>unknown):Promise<unknown>;

Parameters

ParameterTypeDescription
onRejected(error:StorageError) => unknown

Returns:

Promise<unknown>

UploadTask.on()

Listens for events on this task.

Events have three callback functions (referred to asnext,error, andcomplete).

If only the event is passed, a function that can be used to register the callbacks is returned. Otherwise, the callbacks are passed after the event.

Callbacks can be passed either as three separate argumentsor as thenext,error, andcomplete properties of an object. Any of the three callbacks is optional, as long as at least one is specified. In addition, when you add your callbacks, you get a function back. You can call this function to unregister the associated callbacks.

Signature:

on(event:TaskEvent,nextOrObserver?:StorageObserver<UploadTaskSnapshot>|null|((snapshot:UploadTaskSnapshot)=>unknown),error?:((a:StorageError)=>unknown)|null,complete?:Unsubscribe|null):Unsubscribe|Subscribe<UploadTaskSnapshot>;

Parameters

ParameterTypeDescription
eventTaskEventThe type of event to listen for.
nextOrObserverStorageObserver<UploadTaskSnapshot> | null | ((snapshot:UploadTaskSnapshot) => unknown)Thenext function, which gets called for each item in the event stream, or an observer object with some or all of these three properties (next,error,complete).
error((a:StorageError) => unknown) | nullA function that gets called with aStorageError if the event stream ends due to an error.
completeUnsubscribe | null

Returns:

Unsubscribe |Subscribe<UploadTaskSnapshot>

If only the event argument is passed, returns a function you can use to add callbacks (see the examples above). If more than just the event argument is passed, returns a function you can call to unregister the callbacks.

Example 1

**Pass callbacks separately or in an object.**

varnext=function(snapshot){};varerror=function(error){};varcomplete=function(){};// The first example.uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,next,error,complete);// This is equivalent to the first example.uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,{'next':next,'error':error,'complete':complete});// This is equivalent to the first example.varsubscribe=uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED);subscribe(next,error,complete);// This is equivalent to the first example.varsubscribe=uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED);subscribe({'next':next,'error':error,'complete':complete});

Example 2

**Any callback is optional.**

// Just listening for completion, this is legal.uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,null,null,function(){console.log('upload complete!');});// Just listening for progress/state changes, this is legal.uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,function(snapshot){varpercent=snapshot.bytesTransferred/snapshot.totalBytes*100;console.log(percent+"% done");});// This is also legal.uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,{'complete':function(){console.log('upload complete!');}});

Example 3

**Use the returned function to remove callbacks.**

varunsubscribe=uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,function(snapshot){varpercent=snapshot.bytesTransferred/snapshot.totalBytes*100;console.log(percent+"% done");// Stop after receiving one update.unsubscribe();});// This code is equivalent to the above.varhandle=uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED);unsubscribe=handle(function(snapshot){varpercent=snapshot.bytesTransferred/snapshot.totalBytes*100;console.log(percent+"% done");// Stop after receiving one update.unsubscribe();});

UploadTask.pause()

Pauses a currently running task. Has no effect on a paused or failed task.

Signature:

pause():boolean;

Returns:

boolean

True if the operation took effect, false if ignored.

UploadTask.resume()

Resumes a paused task. Has no effect on a currently running or failed task.

Signature:

resume():boolean;

Returns:

boolean

True if the operation took effect, false if ignored.

UploadTask.then()

This object behaves like a Promise, and resolves with its snapshot data when the upload completes.

Signature:

then(onFulfilled?:((snapshot:UploadTaskSnapshot)=>unknown)|null,onRejected?:((error:StorageError)=>unknown)|null):Promise<unknown>;

Parameters

ParameterTypeDescription
onFulfilled((snapshot:UploadTaskSnapshot) => unknown) | nullThe fulfillment callback. Promise chaining works as normal.
onRejected((error:StorageError) => unknown) | nullThe rejection callback.

Returns:

Promise<unknown>

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-01-19 UTC.