File Upload in Angular 4

前端 未结 6 588
我在风中等你
我在风中等你 2021-02-04 14:26

when I\'m trying to install \"npm install ng2-file-upload --save\" in my angular 4 application it throws

UNMET PEER DEPENDENCY @4.1.0
UNMET PEER DEPENDENCY          


        
6条回答
  •  难免孤独
    2021-02-04 14:52

    HTML:

    
    

    TS:

    @Component({
      ......
    })
    
    export class myComponent{
    
        form: FormGroup;
    
        contructor(fb: FormGroup){
           form: fb.group({file: null});
        }
    
     //fVals comes from HTML Form -> (ngSubmit)="postImage(form.value)" 
        postImage(fVals){
          let body = new FormData();
          body.append('file', formValues.file);
    
          let httpRequest = httpclient.post(url, body);
          httpRequest.subscribe((response) =>{
             //..... handle response here
          },(error) => {
             //.....handle errors here
          });
       }
    
       onFileChange(event) {
         if(event.target.files.length > 0) {
           let file = event.target.files[0];
           this.form.get('file').setValue(file);
         }
       }
    }
    

提交回复
热议问题