Guest User

Untitled

a guest
Jul 16th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. @Component({
  2. selector: 'file-upload',
  3. templateUrl: './file-upload.component.html',
  4. styleUrls: ['./file-upload.component.css']
  5. })
  6.  
  7. export class FileUploadComponent {
  8. task: AngularFireUploadTask;
  9. percentage: Observable<number>;
  10. snapshot: Observable<any>;
  11. downloadURL: Observable<string | null> ;
  12.  
  13.  
  14.  
  15.  
  16.  
  17. constructor(private storage: AngularFireStorage, private db: AngularFirestore) {
  18. this.downloadURL = 'http://via.placeholder.com/350x150';
  19. }
  20.  
  21. fileUpload(event){
  22.  
  23. const file = event.target.files[0];
  24.  
  25. const filePath = `test/${new Date().getTime()}_${file.name}`;
  26.  
  27. const fileRef = this.storage.ref(filePath);
  28.  
  29. const task = this.storage.upload(filePath, file);
  30.  
  31. //observe percentage changes
  32. this.percentage = task.percentageChanges();
  33. //get notified when the download URL is available
  34. task.snapshotChanges().pipe(
  35. finalize(
  36. () => {
  37. this.downloadURL = fileRef.getDownloadURL();
  38. }
  39. )
  40. )
  41. .subscribe();
  42.  
  43.  
  44. }
  45.  
  46.  
  47. }
  48.  
  49. <div class="container">
  50. <div class="card">
  51. <div class="card-header">
  52. Firebase Cloud Storage & Angular 5
  53. </div>
  54. <div class="card-body">
  55. <h5 class="card-title">Select a file for upload:</h5>
  56. <input type="file" (change)="fileUpload($event)" accept=".png,.jpg" />
  57. </div>
  58. <div class="progress">
  59. <div class="progress-bar progress-bar-striped bg-success" role="progressbar" [style.width]="(percentage | async) + '%'" [attr.aria-valuenow]="(percentage | async)" aria-valuemin="0" aria-valuemax="100"></div>
  60. </div>
  61. <img *ngIf="downloadURL != null" [src]="downloadURL | async" />
  62. </div>
  63. </div>
  64.  
  65. downloadURL: BehaviorSubject<string>;
  66.  
  67. construction () {
  68. this.downloadURL = new BehaviorSubject<string>('http://via.placeholder.com/350x150');
  69. }
  70.  
  71. task.snapshotChanges().pipe(
  72. finalize(
  73. () => {
  74. const dlImage = fileRef.getDownloadURL();
  75. this.downloadURL.next(dlImage);
  76. }
  77. )
  78. )
  79. .subscribe();
  80.  
  81. <img src="{{downloadURL | async}}" />
Add Comment
Please, Sign In to add comment