[Angularfire] Angular File uploads to Firebase Storage with Angular control value accessor

Source: Internet
Author: User

Tags: contain att tps storage core col Write you rmi

The upload class is used in the service layer. Notice it has a constructor file for attribute, and which has a type of File . This would allows us to initialize new uploads with a JavaScript File object. You'll see what's important in the next step.

Export class Upload {  $key: string;  File:file;  name:string;  url:string;  Progress:number;   New Date ();  Constructor (file:file) {    this. File = file;  }} 

Then build the upload service, which can inject to component:

Import {injectable} from ' @angular/core '; import {Subject} from' Rxjs/subject '; import {Matsnackbar} from' @angular/material '; Import* As Firebase from ' Firebase '; import Uploadtasksnapshot=firebase.storage.uploadtasksnapshot;import {Upload} from'./upload '; @Injectable () Export class Uploadservice {uploading$=NewSubject<number>(); completed$=NewSubject<upload>(); Constructor (private Snackbar:matsnackbar) {} uploadfile (Upload:upload, folder:string) {//Create a storage refConst STORAGEREF =firebase.storage (). ref (); Const Uploadtask= Storageref.child (' ${folder}/${upload.file.name} '). put (Upload.file);//Upload FileUploadtask.on (Firebase.storage.TaskEvent.STATE_CHANGED, (snapshot:uploadtasksnapshot)={upload.progress= (snapshot.bytestransferred/snapshot.totalbytes) * 100;  This. Uploading$.next (upload.progress); }, (Err)= {         This. Snackbar.open (Err.message, ' OK ', {duration:3000,        }); },      () ={Upload.url=UploadTask.snapshot.downloadURL; Upload.name=Upload.file.name;  This. Completed$.next (upload);  This. Uploading$.next (NULL);  }); } deleteupload (Name:string, folder:string) {const STORAGEREF=firebase.storage (). ref (); Storageref.child (' ${folder}/${name} '). Delete (); This. Completed$.next (); }}


Import {changedetectionstrategy, Component, ForwardRef, Input} from ' @angular/core '; import {controlvalueaccessor, ng_value_accessor} from' @angular/forms '; import {Uploadservice} from‘.. /.. /services/upload.service '; import {Upload} from‘.. /.. /services/upload '; import {Observable} from' Rxjs/observable '; export const Type_control_accessor={provide:ng_value_accessor, Multi:true, Useexisting:forwardref (()=imageuploadercomponent)}; @Component ({selector:' Image-uploader ', ChangeDetection:ChangeDetectionStrategy.OnPush, providers: [Type_control_accessor], Templateurl:'./image-uploader.component.html ', Styleurls: ['./image-uploader.component.scss ']}) Export class Imageuploadercomponent implements Controlvalueaccessor {@Input () img;  Private ontouch:function;  Private onmodelchange:function;  Private value:string;  File:upload;  Currentupload:upload; progress$: Observable<number>; Constructor (private Uploadservice:uploadservice) { This. progress$ = This. uploadservice.uploading$;  This. Uploadservice.completed$.subscribe ((upload) = {      if(upload) { This. setselected (Upload.url);  This. Currentupload =upload; } Else {         This. setselected (");  This. Currentupload =NULL;  }    }); } onChange ($event) {Const file= $event. target.files[0];  This. File =NewUpload (file);  This. Uploadservice.uploadfile ( This. File, ' icons '); } writevalue (Value:any):void {     This. Value =value; } registeronchange (fn:function):void {     This. Onmodelchange =fn; } registerontouched (fn:function):void {     This. OnTouch =fn; } setselected (value:string):void {     This. Value =value;  This. Onmodelchange (value);  This. OnTouch (); } clear () {if( This. File) {       This. Uploadservice.deleteupload ( This. File.name, ' icons ');  This. setselected ("); }  }}


    <Div*ngif= "progress$ | async as P">      <Mat-progress-barMode= "Determinate"[value]= "P"></Mat-progress-bar>    </Div>    <Mat-card-subtitle>select/upload icon</Mat-card-subtitle>    <mat-card-contentFxlayout= "column">      <DivFxlayout= "Row"fxlayoutalign= "Space-around">        <Div*ngif= "Currentupload"class= "Image-container"Fxflex= "30%">          <img[SRC]= "Currentupload?". URL | | ' ' "[Alt]= "Currentupload?". name | | ' ' ">        </Div>      </Div>

[Angularfire] Angular File uploads to Firebase Storage with Angular control value accessor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: