ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.3k

I get error property name when upload data ?

Nov 22 2020 8:40 PM
I work on angular 7 project  I face issue I get error Name when upload data 
 
  1. core.js:4197 ERROR TypeError: Cannot read property 'name' of undefined  
  2.     at UploadComponent.uploadFile (upload.component.ts:96)  
  3.     at UploadComponent_Template_button_click_5_listener (upload.component.html:6)  
  4.     at executeListenerWithErrorHandling (core.js:14286)  
  5.     at wrapListenerIn_markDirtyAndPreventDefault (core.js:14321)  
  6.     at HTMLButtonElement.<anonymous> (platform-browser.js:582)  
  7.     at ZoneDelegate.invokeTask (zone-evergreen.js:399)  
  8.     at Object.onInvokeTask (core.js:27394)  
  9.     at ZoneDelegate.invokeTask (zone-evergreen.js:398)  
  10.     at Zone.runTask (zone-evergreen.js:167)  
  11.     at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:480)  
I get this error on line below :
 
  1. formData.append('file', fileToUpload, fileToUpload.name);  
 full uplod code 
 
component.html
  1. <div class="col-md-3">  
  2.    <input type="file" #file placeholder="Choose file"  multiple>  
  3.      
  4.      
  5.    <button type="button" class="btn btn-success" (click)="uploadFile(file)">Upload File</button>  
  6.  </div>  
 component.ts
  1. public uploadFile = (files) => {  
  2.     if (files.length === 0) {  
  3.       return;  
  4.     }  
  5.   
  6.     let fileToUpload = <File>files[0];  
  7.     const formData = new FormData();  
  8.     formData.append('file', fileToUpload, fileToUpload.name);  
  9.       
  10.     this.http.post('https://localhost:44396/api/ApprovalQuality/', formData, {reportProgress: true, observe: 'events'})  
  11.       .subscribe(event => {  
  12.         console.log("success");  
  13.         console.log(event.type);  
  14.         if (event.type === HttpEventType.UploadProgress)  
  15.         {  
  16.          console.log("progress");  
  17.           this.progress = Math.round(100 * event.loaded / event.total);  
  18.         }  
  19.         else if (event.type === HttpEventType.Response) {  
  20.           this.message = 'Upload success.';  
  21.           this.onUploadFinished.emit(event.body);  
  22.         }  
  23.       });  
  24.   }  
so How to solve this error please ? 

Answers (1)