Skip to content

Commit 8678dbe

Browse files
committed
Updated Storage guide.
1 parent ac32095 commit 8678dbe

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

examples/UploadWithProgress.mxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
3434
/*
3535
We prepare the URLRequest that will be sent in the upload method.
36-
The file will be sent as a String that represnets the bytes from the file.
36+
The file will be sent as a String that represents the bytes from the file.
3737
We are using uploadUnencoded instead of upload since we have already encoded our data by sending it as a String.
3838
Finally, we tell the PopUpManager to add our custom popup and center it.
3939
*/

storage/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ private function uploadFile():void
116116
117117
var fileStream:FileStream = new FileStream();
118118
fileStream.open(file, FileMode.READ);
119+
119120
var bytes:ByteArray = new ByteArray();
120121
fileStream.readBytes(bytes);
121122
fileStream.close();
@@ -157,6 +158,48 @@ Your new file and a `savegames` folder will instantly appear in the Storage sect
157158

158159
The `contentType` doesn't need to be accurate, but it is recommended to set it properly.
159160

161+
## Uploading with Progress Indicator
162+
163+
You can also upload files using the `upload` and `uploadUnencoded` methods from the `File` and `FileReference` classes.
164+
165+
This example will demonstrate how to upload a file from a fixed location and retrieve the upload progress.
166+
167+
```actionscript
168+
private function uploadFile():void
169+
{
170+
var file:File = File.applicationStorageDirectory.resolvePath("heavy_picture.jpg");
171+
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
172+
ile.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);
173+
174+
var fileStream:FileStream = new FileStream();
175+
fileStream.open(file, FileMode.READ);
176+
177+
var bytes:ByteArray = new ByteArray();
178+
fileStream.readBytes(bytes);
179+
fileStream.close();
180+
181+
var request:URLRequest = new URLRequest("https://firebasestorage.googleapis.com/v0/b/<YOUR-PROJECT-ID>.appspot.com/o/pictures%2F"+"heavy_picture.jpg");
182+
request.method = URLRequestMethod.POST;
183+
request.data = bytes.toString();
184+
request.contentType = "image/jpeg";
185+
186+
file.uploadUnencoded(request);
187+
}
188+
189+
private function progressHandler(event:ProgressEvent):void
190+
{
191+
var progress:Number = Math.round((event.bytesLoaded/event.bytesTotal)*100);
192+
trace("Upload Progress: " + progress + "%");
193+
}
194+
195+
private function uploadCompleteDataHandler(event:DataEvent):void
196+
{
197+
trace(event.data); //Here you will receive the file metadata from Firebase Storage.
198+
}
199+
```
200+
201+
It is required to send the file as a `String` that represents the file bytes and use the `uploadEncoded` method.
202+
160203
## Uploading with Auth
161204

162205
Authorizing requests for Firebase Storage is a bit different than in Firebase Database. Instead of adding an `auth` parameter in the URL with the `authToken`, we add it into a header.

0 commit comments

Comments
 (0)