Represents the process of uploading an object. Allows you to monitor andmanage the upload.
Index
Properties
snapshot
A snapshot of the current task state.
Methods
cancel
- cancel():boolean
Cancels a running task. Has no effect on a complete or failed task.
Returnsboolean
True if the cancel had an effect.
catch
- catch(onRejected: (error: FirebaseStorageError) =>any):Promise<any>
Equivalent to calling
then(null, onRejected).Parameters
onRejected:(error:FirebaseStorageError) =>any
- (error:FirebaseStorageError):any
Parameters
error:FirebaseStorageError
Returnsany
ReturnsPromise<any>
on
- on(event: TaskEvent, nextOrObserver?: StorageObserver<UploadTaskSnapshot> |null |((snapshot: UploadTaskSnapshot) =>any), error?: ((error: FirebaseStorageError) =>any) |null, complete?: firebase.Unsubscribe |null):Function
Listens for events on this task.
Events have three callback functions (referred to as
next,error, andcomplete).If only the event is passed, a function that can be used to register thecallbacks is returned. Otherwise, the callbacks are passed after the event.
Callbacks can be passed either as three separate argumentsor as the
next,error, andcompleteproperties of an object. Any of the threecallbacks 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 thisfunction to unregister the associated callbacks.- example
Pass callbacks separately or in an object.
var next =function(snapshot){};var error =function(error){};var complete =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.var subscribe = uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED);subscribe(next, error, complete);// This is equivalent to the first example.var subscribe = uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED);subscribe({'next': next,'error': error,'complete': complete});- example
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){var percent = 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
Use the returned function to remove callbacks.
var unsubscribe = uploadTask.on( firebase.storage.TaskEvent.STATE_CHANGED,function(snapshot){var percent = snapshot.bytesTransferred / snapshot.totalBytes *100;console.log(percent +"% done");// Stop after receiving one update. unsubscribe(); });// This code is equivalent to the above.var handle = uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED);unsubscribe = handle(function(snapshot){var percent = snapshot.bytesTransferred / snapshot.totalBytes *100;console.log(percent +"% done");// Stop after receiving one update. unsubscribe();});
Parameters
event:TaskEvent
The event to listen for.
Optional nextOrObserver:StorageObserver<UploadTaskSnapshot> |null |((snapshot:UploadTaskSnapshot) =>any)
The `next` function, which gets calledfor each item inthe event stream, or an observerobjectwithsomeorallofthesethreeproperties (`next`, `error`, `complete`).Optional error:((error:FirebaseStorageError) =>any) |null
A function that gets called with a
FirebaseStorageErrorif the event stream ends due to an error.Optional complete:firebase.Unsubscribe |null
A function that gets called if theevent stream ends normally.
ReturnsFunction
Ifonly the event argumentis passed,returns afunction you can usetoadd callbacks (see the examples above).If more than just the eventargumentis passed,returns afunction you cancallto unregister thecallbacks.
pause
- pause():boolean
Pauses a running task. Has no effect on a paused or failed task.
Returnsboolean
True if the pause had an effect.
resume
- resume():boolean
Resumes a paused task. Has no effect on a running or failed task.
Returnsboolean
True if the resume had an effect.
then
- then(onFulfilled?: ((snapshot: UploadTaskSnapshot) =>any) |null, onRejected?: ((error: FirebaseStorageError) =>any) |null):Promise<any>
This object behaves like a Promise, and resolves with its snapshot data whenthe upload completes.
Parameters
Optional onFulfilled:((snapshot:UploadTaskSnapshot) =>any) |null
The fulfillment callback. Promise chaining worksasnormal.Optional onRejected:((error:FirebaseStorageError) =>any) |null
The rejection callback.
ReturnsPromise<any>
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 2022-07-27 UTC.