ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.3k

How to pass number from angular to web api when action post asp.net c

May 27 2021 3:18 PM
I work on asp.net core 3.1 web api  server side 
 
and angular 7 client side 
 
I work on page make upload to single file and it is ok working but when try to pass Number flag with file uploaded 
 
I face issue I can't pass value of variable selectedOwnerLevel  from angular 7 to web api
selectedOwnerLevel: number = 1; 
 
suppose selectedOwnerLevel is have value 1
 
so How to pass value 1 to web api  
 
on angular 7 client side
  1. public uploadFile = (files,selectowner) => {  
  2.      
  3.   const formData = new FormData();  
  4.     
  5.   formData.append('file'this.fileToUpload,this.fileToUpload.name);  
  6.    
  7.  console.log(this.selectedOwnerLevel);  // it have here 5
  8.  if(this.selectedOwnerLevel==1)  
  9.  {  
  10.  this.http.post('http://localhost:61265/api/DeliverySys/', formData,{ responseType: 'blob' })  
  11.     .subscribe((response: Blob) => saveAs(response, this.fileToUpload.name + '.xlsx'));  
  12.  }  
  13.        
  14. }  
on html :
  1. <div class="form-group">    
  2.      <label for="file">Choose File</label>    
  3.      <input type="file"    
  4.             id="file"    
  5.             (change)="handleFileInput($event.target.files)">    
  6.             <button type="button" class="btn btn-success" (click)="uploadFile(files,this.selectedOwnerLevel)">Upload File</button>  
  7.  </div>   
 
on server side web api
  1. [HttpPost, DisableRequestSizeLimit]  
  2.         public IActionResult Upload(int selectedOwnerLevel)  
  3.         {  
  4.             try  
  5.             {  
  6.                 var DisplayFileName = Request.Form.Files[0];  
  7.                 string fileName = DisplayFileName.FileName.Replace(".xlsx""-") + Guid.NewGuid().ToString() + ".xlsx";  
  8.   
  9.             }  
  10.             catch (Exception ex)  
  11.             {  
  12.                 return StatusCode(500, $"Internal server error: {ex}");  
  13.             }  
 when debug value of selectedOwnerLevel it display as 0 
 
so please How to display as actual value 1 on angular 7 ?
when do consol.log (this.selectedOwnerLevel) it give me 1 on image below 
but it display 0 
so How to solve issue please ? 
 
 
 
thanks  

Answers (3)