ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.3k

error Cannot read property 'files' of undefined when upload files

Jun 3 2021 1:01 AM

I work on angular 7 project

I face issue when select file then press button upload to get selected file

then it show error Cannot read property 'files' of undefined

as below

  1. DeliverySystemComponent.html:25 ERROR TypeError: Cannot read property 'files' of undefined  
  2.     at Object.eval [as handleEvent] (DeliverySystemComponent.html:26)  
  3.     at handleEvent (core.js:29239)  
  4.     at callWithDebugContext (core.js:30309)  
  5.     at Object.debugHandleEvent [as handleEvent] (core.js:30036)  
  6.     at dispatchEvent (core.js:19859)  
  7.     at core.js:21044  
  8.     at SafeSubscriber.schedulerFn [as _next] (core.js:23747)  
  9.     at SafeSubscriber.__tryOrUnsub (Subscriber.js:194)  
  10.     at SafeSubscriber.next (Subscriber.js:132)  
  11.     at Subscriber._next (Subscriber.js:76)  
  12. View_DeliverySystemComponent_0 @ DeliverySystemComponent.html:25  

and that actually my issue

so How to solve this issue

what I try is

delivery html component

  1. <app-custom-upload #cusUpload nbTooltip="upload excel file" nbTooltipPlacement="bottom"    
  2.       nbTooltipStatus="primary" (complete)="handleFileInput($event.target.files)"></app-custom-upload>    
  3.     
  4. <button (click)="uploadFile(files)" class="btn btn-link" style="margin-right: 0px">    
  5.           <i nbTooltip="Import Template" nbTooltipPlacement="bottom" nbTooltipStatus="primary">    
  6.             <nb-icon icon="cloud-upload-outline"></nb-icon>    
  7.           </i>    
  8. </button>    

delivery component type script

  1. fileToUpload: File = null;  
  2. handleFileInput(files: FileList) {  
  3.   if (files.length == 0) {  
  4.       return;  
  5.     }    
  6.    
  7.   this.fileToUpload = files.item(0);  
  8.    
  9. }   
  10. public uploadFile = (files) => {  
  11.      
  12.   const formData = new FormData();  
  13.     
  14.   formData.append('file'this.fileToUpload,this.fileToUpload.name);  
  15.  }  
upload file error