Skip to main content

Storage - Download Files

Download Files#

To download a file, first create a Firebase Storage reference to the file you want to download.

var reference:StorageReference = FirebaseStorage.service.getReference().child( "downloads/example.png" );

Download Files#

Once you have a reference, you can download files from Firebase Storage by calling the getBytes() or getFile(). You can also download the file directly from the URL using a standard AS3 loader.

Download in memory#

Download the file to a ByteArray with the getBytes() method. This is the easiest way to download a file, but it must load the entire contents of your file into memory.

If you request a file larger than your app's available memory, your app will crash. To protect against memory issues, getBytes() takes a maximum amount of bytes to download. Set the maximum size to something you know your app can handle, or use another download method.

var task:DownloadTask = reference.getBytes( 1024*1024 );

Note: DownloadTask retrieved from getBytes cannot be paused or canceled and will not dispatch the equivalent progress or paused events.

To get access to the byte data you should listen for the SUCCESS event:

task.addEventListener( DownloadTaskEvent.SUCCESS, downloadSuccessHandler );

In your handler you will have access to the byte data via the taskSnapshot:

private function downloadSuccessHandler( event:DownloadTaskEvent ):void
var data:ByteArray = event.taskSnapshot.bytes;

Download to a local file#

The getFile() method downloads a file directly to a local device.

Use this if your users want to have access to the file while offline or to share the file in a different app. getFile() returns a DownloadTask which you can use to manage your download and monitor the status of the download.

var file:File = File.applicationStorageDirectory.resolvePath( "downloads/test.png" );
var task:DownloadTask = reference.getFile( file );

A DownloadTask from getFile will have access to all the functionality defined including progress and paused events.

Monitor Download Progress#

A download task dispatches DownloadTaskEvents to indicate completion, progress, and errors:

task.addEventListener( DownloadTaskEvent.SUCCESS, downloadSuccessHandler );
task.addEventListener( DownloadTaskEvent.ERROR, downloadErrorHandler );

Events are dispatched with a DownloadTaskSnapshot object. This object is an immutable view of the task at the time the event occurred.

private function downloadSuccessHandler( event:DownloadTaskEvent ):void
// event.taskSnapshot contains details about the completed download
private function downloadErrorHandler( event:DownloadTaskEvent ):void
// Handle unsuccessful download
// event.errorMessage contains the reason for the error

Manage Downloads#

In addition to starting downloads, you can pause, resume, and cancel uploads using the pause(), resume(), and cancel() methods. Pause and resume events raise pause and progress state changes respectively. Canceling a download causes the download to fail with an error indicating that the download was canceled.

// Pause the download
// Resume the download
// Cancel the download