message  Send Feedback

Task

Storage Task used for Uploading or Downloading files.

Example 1

Get a Upload Storage task to upload a string:

const string = '{ "foo": 1 }';
const task = firebase
.storage()
.ref('/foo/bar.json')
.putString(string);

Example 2

Get a Download Storage task to download a file:

const downloadTo = `${firebase.utils.FilePath.DOCUMENT_DIRECTORY}/bar.json`;
const task = firebase
.storage()
.ref('/foo/bar.json')
.writeToFile(downloadTo);

Methods


Methods

cancel

cancel(): Promise<boolean>;

Cancel the current Download or Upload task.

Example

Cancel a task inside a state changed listener:

task.on('state_changed', taskSnapshot => {
console.log('Cancelling my task!');
task.cancel();
});

catch

catch(
  onRejected: (a: NativeFirebaseError) => any
): Promise<any>;

Parameters


on

on(
  event: 'state_changed',
  nextOrObserver?: TaskSnapshotObserver | null | (a: TaskSnapshot) => any,
  error?: (a: NativeFirebaseError) => any | null,
  complete?: () => void | null
): () => void;

Task event handler called when state has changed on the task.

Example

const task = firebase
.storage()
.ref('/foo/bar.json')
.writeToFile(downloadTo);
task.on('state_changed', (taskSnapshot) => {
console.log(taskSnapshot.state);
})
.then(() => {]
console.log('Task complete');
})
.catch((error) => {
console.error(error.message);
});

Parameters

  • event: 'state_changed'
    The event name to handle, always `state_changed`.
  • nextOrObserver: (optional) TaskSnapshotObserver | null | (a: TaskSnapshot) => any
    The optional event observer function.
  • error: (optional) (a: NativeFirebaseError) => any | null
    An optional JavaScript error handler.
  • complete: (optional) () => void | null
    An optional complete handler function.

pause

pause(): Promise<boolean>;

Pause the current Download or Upload task.

Example

Pause a running task inside a state changed listener:

task.on('state_changed', taskSnapshot => {
if (taskSnapshot.state === firebase.storage.TaskState.RUNNING) {
console.log('Pausing my task!');
task.pause();
}
});

resume

resume(): Promise<boolean>;

Resume the current Download or Upload task.

Example

Resume a previously paused task inside a state changed listener:

task.on('state_changed', taskSnapshot => {
// ... pause me ...
if (taskSnapshot.state === firebase.storage.TaskState.PAUSED) {
console.log('Resuming my task!');
task.resume();
}
});

then

then(
  onFulfilled?: (a: TaskSnapshot) => any | null,
  onRejected?: (a: NativeFirebaseError) => any | null
): Promise<any>;

Parameters


React Native Market

Looking for pre-built React Native apps to kick start your next project? Check out the React Native Market by Invertase.

Your purchases help support our open-source projects such as React Native Firebase. All items are currently 50% off to celebrate the release of React Native Firebase version 6.
shopping_cartVisit React Native Market