Skip to content

Commit 5dcc354

Browse files
renansigolojamesdaniels
authored andcommitted
docs(storage): put, putString, upload, and downloadURL docs (angular#1650)
1 parent 26f3f5f commit 5dcc354

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

docs/storage/storage.md

+68-11
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,62 @@ export class AppComponent {
4545

4646
### Uploading blobs
4747

48-
There are two options for uploading files.
48+
There are three options for uploading files.
4949

5050

51-
| method | |
52-
| ---------|--------------------|
53-
| `put(data: Blob, metadata?: storage.UploadMetadata): AngularFireUploadTask` | Starts the upload of the blob to the storage reference's path. Returns an `AngularFireUploadTask` for upload monitoring. |
51+
| method | |
52+
| ---------|--------------------|
53+
| `put(data: Blob, metadata?: storage.UploadMetadata): AngularFireUploadTask` | Starts the upload of the blob to the storage reference's path. Returns an `AngularFireUploadTask` for upload monitoring. |
5454
| `putString(data: string, format?: StringFormat, metadata?: UploadMetadata): AngularFireUploadTask` | Updates an existing item in the array. Accepts a key, database reference, or an unwrapped snapshot. |
55+
| `upload(path: string, data: StringFormat, metadata?: UploadMetadata): AngularFireUploadTask` | Upload or update a new file to the storage reference's path. Returns an `AngularFireUploadTask` for upload monitoring. |
56+
57+
#### Uploading blobs with put
58+
59+
```ts
60+
import { Component } from '@angular/core';
61+
import { AngularFireStorage } from 'angularfire2/storage';
62+
63+
@Component({
64+
selector: 'app-root',
65+
template: `
66+
<input type="file" (change)="uploadFile($event)">
67+
`
68+
})
69+
export class AppComponent {
70+
constructor(private storage: AngularFireStorage) { }
71+
uploadFile(event) {
72+
const file = event.target.files[0];
73+
const filePath = 'name-your-file-path-here';
74+
const ref = this.storage.ref(filePath);
75+
const task = ref.put(file);
76+
}
77+
}
78+
```
79+
80+
#### Uploading blobs with putString
81+
82+
```ts
83+
import { Component } from '@angular/core';
84+
import { AngularFireStorage } from 'angularfire2/storage';
85+
86+
@Component({
87+
selector: 'app-root',
88+
template: `
89+
<input type="file" (change)="uploadFile($event)">
90+
`
91+
})
92+
export class AppComponent {
93+
constructor(private storage: AngularFireStorage) { }
94+
uploadFile(event) {
95+
const file = event.target.files[0];
96+
const filePath = 'name-your-file-path-here';
97+
const ref = this.storage.ref(filePath);
98+
const task = ref.putString(file);
99+
}
100+
}
101+
```
102+
103+
#### Uploading files with upload
55104

56105
```ts
57106
import { Component } from '@angular/core';
@@ -77,15 +126,19 @@ export class AppComponent {
77126

78127
An `AngularFireUploadTask` has methods for observing upload percentage as well as the final download URL.
79128

80-
| method | |
81-
| ---------|--------------------|
82-
| `snapshotChanges(): Observable<FirebaseStorage.UploadTaskSnapshot>` | Emits the raw `UploadTaskSnapshot` as the file upload progresses. |
83-
| `percentageChanges(): Observable<number>` | Emits the upload completion percentage. |
84-
| `downloadURL(): Observable<string>` | Emits the download url when available |
129+
| method | |
130+
| ---------|--------------------|
131+
| `snapshotChanges(): Observable<FirebaseStorage.UploadTaskSnapshot>` | Emits the raw `UploadTaskSnapshot` as the file upload progresses. |
132+
| `percentageChanges(): Observable<number>` | Emits the upload completion percentage. |
133+
| `getDownloadURL(): Observable<any>` | Emits the download url when available |
85134

86135
#### Example Usage
87136

137+
The method `getDownloadURL()` doesn't rely on the task anymore, hence, in order to get the url we should use the finalize method from RxJS on top of the storage ref.
138+
88139
```ts
140+
import { finalize } from 'rxjs/operators';
141+
89142
@Component({
90143
selector: 'app-root',
91144
template: `
@@ -101,12 +154,16 @@ export class AppComponent {
101154
uploadFile(event) {
102155
const file = event.target.files[0];
103156
const filePath = 'name-your-file-path-here';
157+
const fileRef = this.storage.ref(filePath);
104158
const task = this.storage.upload(filePath, file);
105-
159+
106160
// observe percentage changes
107161
this.uploadPercent = task.percentageChanges();
108162
// get notified when the download URL is available
109-
this.downloadURL = task.downloadURL();
163+
task.snapshotChanges().pipe(
164+
finalize(() => this.downloadURL = fileRef.getDownloadURL() )
165+
)
166+
.subscribe()
110167
}
111168
}
112169
```

0 commit comments

Comments
 (0)