|
1 |
| -import { Observable, EventData, Page } from '@nativescript/core'; |
| 1 | +import { Observable, EventData, Page, File, knownFolders, path } from '@nativescript/core'; |
2 | 2 | import { DemoSharedFirebaseStorage } from '@demo/shared';
|
3 | 3 | import { Metadata, Storage, TaskEvent } from '@nativescript/firebase-storage';
|
4 | 4 | import { Auth } from '@nativescript/firebase-auth';
|
@@ -38,11 +38,42 @@ export class DemoModel extends DemoSharedFirebaseStorage {
|
38 | 38 | },
|
39 | 39 | next(snapshot) {
|
40 | 40 | console.info('uploadText', 'next', 'state', snapshot.state, snapshot.error);
|
41 |
| - console.log('uploadText', 'next', 'progress %', snapshot.bytesTransferred / snapshot.totalBytes ?? 0 * 100); |
| 41 | + if (snapshot.totalBytes) { |
| 42 | + console.log('uploadFile', 'next', 'progress %', snapshot.bytesTransferred / snapshot.totalBytes ?? 0 * 100); |
| 43 | + } |
42 | 44 | if (snapshot.error) {
|
43 | 45 | console.error('uploadText', 'next', snapshot.error);
|
44 | 46 | }
|
45 | 47 | },
|
46 | 48 | });
|
47 | 49 | }
|
| 50 | + |
| 51 | + uploadFile() { |
| 52 | + const file = File.fromPath( |
| 53 | + path.join(knownFolders.currentApp().path, 'images', 'deadpool.jpeg') |
| 54 | + ) |
| 55 | + const metadata = new Metadata(); |
| 56 | + metadata.contentType = 'image/jpg'; |
| 57 | + this.storage |
| 58 | + .ref() |
| 59 | + .child('images/deadpool.jpeg') |
| 60 | + .putFile(file.path, metadata) |
| 61 | + .on(TaskEvent.STATE_CHANGED, { |
| 62 | + complete() { |
| 63 | + console.info('uploadFile', 'completed'); |
| 64 | + }, |
| 65 | + error(err) { |
| 66 | + console.error('uploadFile', 'error', err); |
| 67 | + }, |
| 68 | + next(snapshot) { |
| 69 | + console.info('uploadFile', 'next', 'state', snapshot.state); |
| 70 | + if (snapshot.totalBytes) { |
| 71 | + console.log('uploadFile', 'next', 'progress %', snapshot.bytesTransferred / snapshot.totalBytes ?? 0 * 100); |
| 72 | + } |
| 73 | + if (snapshot.error) { |
| 74 | + console.error('uploadFile', 'next', snapshot.error); |
| 75 | + } |
| 76 | + }, |
| 77 | + }); |
| 78 | + } |
48 | 79 | }
|
0 commit comments