firebase.storage. UploadTask
Represents the process of uploading an object. Allows you to monitor and manage the upload.
Property
snapshot
non-null firebase.storage.UploadTaskSnapshot
A snapshot of the current task state.
Methods
cancel
cancel() returns boolean
Cancels a running task. Has no effect on a complete or failed task.
- Returns
-
booleanTrue if the cancel had an effect.
catch
catch(onRejected) returns firebase.Promise
Equivalent to calling then(null, onRejected).
Parameter |
|
|---|---|
|
onRejected |
function(non-null Error) Value must not be null. |
- Returns
-
non-null firebase.Promise
on
on(event, nextOrObserver, error, complete) returns (function() or function())
Listens for events on this task.
Events have three callback functions (referred to as next, error, and
complete).
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 arguments or as the
next, error, and complete 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.
Parameter |
|
|---|---|
|
event |
The event to listen for. Value must not be null. |
|
nextOrObserver |
Optional (nullable function(non-null Object) or non-null Object) The |
|
error |
Optional function(non-null Error) A function that gets called with an Error if the event stream ends due to an error. Value may be null. |
|
complete |
Optional function() A function that gets called if the event stream ends normally. Value may be null. |
- Returns
-
(non-null function() or non-null function(nullable function(non-null Object), optional nullable function(non-null Error), optional nullable function()) returns non-null function())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.
Examples
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
});
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!');
}
});
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();
});
pause
pause() returns boolean
Pauses a running task. Has no effect on a paused or failed task.
- Returns
-
booleanTrue if the pause had an effect.
resume
resume() returns boolean
Resumes a paused task. Has no effect on a running or failed task.
- Returns
-
booleanTrue if the resume had an effect.
then
then(onFulfilled, onRejected) returns firebase.Promise
This object behaves like a Promise, and resolves with its snapshot data when the upload completes.
Parameter |
|
|---|---|
|
onFulfilled |
Optional nullable function(non-null firebase.storage.UploadTaskSnapshot) returns any type The fulfillment callback. Promise chaining works as normal. |
|
onRejected |
Optional nullable function(non-null Error) returns any type The rejection callback. |
- Returns
-
non-null firebase.Promise

