Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

E.4 Download Files

myflashlab edited this page Sep 7, 2016 · 2 revisions

Download files from Firebase Storage

Firebase Storage allows developers to quickly and easily download files from a Google Cloud Storage bucket provided and managed by Firebase.

Note: By default, Firebase Storage buckets require Firebase Authentication to upload files. You can change your Firebase Storage Security Rules to allow unauthenticated access. Since the default Google App Engine app and Firebase share this bucket, configuring public access may make newly uploaded App Engine files publicly accessible as well. Be sure to restrict access to your Storage bucket again when you set up authentication.

Create a Reference

To download a file, first create a Firebase Storage reference to the file you want to download. You can create a reference by appending child paths to the storage root, or you can create a reference from an existing gs:// or https:// URL referencing an object in Firebase Storage.

Download Files

Once you have a reference, you can download files from Firebase Storage in three ways:

  1. Download to ByteArray in memory.
  2. Download to a File representing a file on device.
  3. Generate a URL representing the file online.

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 rootRef:StorageReference = Storage.getReference();
var imgRef:StorageReference = rootRef.child("images/img.png");

// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
imgRef.getBytes(1024*1024, onGetBytesSuccess, onGetBytesFailed);

function onGetBytesFailed(e:StorageEvents):void
{
	trace("onGetBytesFailed, errorCode = " + e.errorCode + " with message: " + e.msg);
}

function onGetBytesSuccess(e:StorageEvents):void
{
	var loader:Loader = new Loader();
	loader.loadBytes(e.bytes);
	addChild(loader);
}

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 FileDownloadTask which you can use to manage your download and monitor the status of the download.

var rootRef:StorageReference = Storage.getReference();
var imgRef:StorageReference = rootRef.child("images/img.png");

// deside where you want to save the downloaded file. Note that this location must be writable.
var distination:File = File.documentsDirectory.resolvePath("img.png");

var fileDownloadTask:FileDownloadTask = imgRef.getFile(distination);

// monitor the download progress with the following listeners
fileDownloadTask.addEventListener(StorageEvents.TASK_FAILED, onDownloadFileFailed);
fileDownloadTask.addEventListener(StorageEvents.TASK_PROGRESS, onDownloadFileProgress);
fileDownloadTask.addEventListener(StorageEvents.TASK_SUCCESS, onDownloadFileSuccess);

function onDownloadFileFailed(e:StorageEvents):void
{
	// make sure to remove the listeners when they are no longer needed
	
	trace("onDownloadFileFailed with errorCode '" + e.errorCode + "' and msg: " + e.msg);
}

function onDownloadFileProgress(e:StorageEvents):void
{
	var percent:Number = fileDownloadTask.bytesTransferred / fileDownloadTask.bytesTotal * 100;
	trace("onDownloadFileProgress = " + Math.floor(percent) + "%");
}

function onDownloadFileSuccess(e:StorageEvents):void
{
	// make sure to remove the listeners when they are no longer needed
	
	trace("onDownloadFileSuccess");
}

Download Data via URL

If you already have download infrastructure based around URLs, or just want a URL to share, you can get the download URL for a file by calling the getDownloadUrl() method on a storage reference.

imgRef.getDownloadUrl(onGetDownloadUrlSuccess, onGetDownloadUrlFailed);

function onGetDownloadUrlFailed(e:StorageEvents):void
{
	trace("onGetDownloadUrlFailed with errorCode '" + e.errorCode + "' and msg: " + e.msg);
}

function onGetDownloadUrlSuccess(e:StorageEvents):void
{
	trace("onGetDownloadUrlSuccess url = " + e.url);
}

Introduction to Firebase ANEs collection for Adobe Air apps


Get Started with Firebase Core in AIR

  1. Prerequisites
  2. Add Firebase to your app
  3. Add the Firebase SDK
  4. Init Firebase Core
  5. Available ANEs
  6. Managing Firebase iid

Get Started with Analytics

  1. Add Analytics ANE
  2. Init Analytics ANE
  3. Log Events
  4. Set User Properties

Get Started with Crashlytics

  1. Add Crashlytics ANE
  2. Test Your Implementation
  3. Customize Crash Reports
  4. Upload .dSYM for iOS apps

Get Started with DynamicLinks

  1. Add DynamicLinks ANE
  2. Init DynamicLinks ANE
  3. Create DynamicLinks
  4. Receive DynamicLinks
  5. View Analytics

Get Started with Authentication

  1. Add Authentication
  2. Init Authentication
  3. Manage Users
  4. Phone Number
  5. Custom Auth
  6. Anonymous Auth
  7. State in Email Actions
  8. Email Link Authentication

Get Started with FCM + OneSignal

  1. Add FCM ANE
  2. Init FCM ANE
  3. Send Your 1st Message
  4. Send Msg to Topics
  5. Understanding FCM Messages
  6. init OneSignal

Get Started with Firestore

  1. Add Firestore
  2. Init Firestore
  3. Add Data
  4. Transactions & Batches
  5. Delete Data
  6. Manage the Console
  7. Get Data
  8. Get Realtime Updates
  9. Simple and Compound
  10. Order and Limit Data
  11. Paginate Data
  12. Manage Indexes
  13. Secure Data
  14. Offline Data
  15. Where to Go From Here

Get Started with Realtime Database

  1. Add Realtime Database
  2. Init Realtime Database
  3. Structure Your Database
  4. Save Data
  5. Retrieve Data
  6. Enable Offline Capabilities

Get Started with Remote Config

  1. Parameters and Conditions
  2. Add Remote Config
  3. Init Remote Config

Get Started with Performance

  1. Add Performance ANE
  2. Init & Start Monitoring

Get Started with Storage

  1. Add Storage ANE
  2. Init Storage ANE
  3. Upload Files to Storage
  4. Download Files to Air
  5. Use File Metadata
  6. Delete Files

Get Started with Functions

  1. Write & Deploy Functions
  2. Add Functions ANE
  3. Init Functions
Clone this wiki locally