Click here to Skip to main content
15,441,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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

TypeScript
<pre>public uploadFile = (files,selectowner) => {  
     
  const formData = new FormData();  
    
  formData.append('file', this.fileToUpload,this.fileToUpload.name);  
   
 console.log(this.selectedOwnerLevel);  // it have here 5
 if(this.selectedOwnerLevel==1)  
 {  
 this.http.post('http://localhost:61265/api/DeliverySys/', formData,{ responseType: 'blob' })  
    .subscribe((response: Blob) => saveAs(response, this.fileToUpload.name + '.xlsx'));  
 }  
       
}

on html :

TypeScript
<div class="form-group">    
     <label for="file">Choose File</label>    
     <input type="file"    
            id="file"    
            (change)="handleFileInput($event.target.files)">    
            <button type="button" class="btn btn-success" (click)="uploadFile(files,this.selectedOwnerLevel)">Upload File</button>  
 </div>

on server side web api

C#
[HttpPost, DisableRequestSizeLimit]  
        public IActionResult Upload(int selectedOwnerLevel)  
        {  
            try  
            {  
                var DisplayFileName = Request.Form.Files[0];  
                string fileName = DisplayFileName.FileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";  
  
            }  
            catch (Exception ex)  
            {  
                return StatusCode(500, $"Internal server error: {ex}");  
            }


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 ?

What I have tried:

formData.append("selectedOwner", this.selectedOwnerLevel);
Posted
Updated 27-May-21 21:28pm

1 solution

The only data you send to the server is the contents of the FormData object, and any data you pass in the querystring.

Your URL has no querystring. Your FormData object contains nothing but the file. Therefore, the server has nothing to bind the selectedOwnerLevel parameter to.

If you want to pass the value to the server, then you need to pass the value to the server.
JavaScript
formData.append('file', this.fileToUpload, this.fileToUpload.name);
formData.append('selectedOwnerLevel', this.selectedOwnerLevel);
 
Share this answer
 
Comments
ahmed_sa 28-May-21 3:35am     CRLF
thank you for reply i need to pass value of this.selectedOwnerLevel on angular 7 to web api variable selectedOwnerLevel public IActionResult Upload(int selectedOwnerLevel) this actually what i need any information need i can tell you on angular 7 i make console.log(this.selectedOwnerLevel) it give me 1 but on web api value of selectedOwnerLevel is 0 so How to solve this issue please help me by any way i need to get value of this.selectedOwnerLevel on server web api so How to do that
Richard Deeming 28-May-21 3:36am     CRLF
Did you even read my answer?! You are NOT passing the variable from Angular to the server. There is nothing for the server to bind to. Pass the value in the FormData collection, as I showed you.
ahmed_sa 28-May-21 9:28am     CRLF
thanks but i have two questions first question how to get value on web api meaning how to get value of this.selectedOwnerLevel on web api second question how to add this this.selectedOwnerLevel on form data i do as below formData.append('selectedOwnerLevel', this.selectedOwnerLevel); when make console.log(FormData) it give me as below formData.append('file', this.fileToUpload,this.fileToUpload.name); formData.append('selectedOwnerLevel',this.selectedOwnerLevel.toString()); console.log(formData); when inspect it come as below file [object File] selectedOwnerLevel 1 so How to access value of selectedOwnerLevel on web api
Richard Deeming 28-May-21 10:10am     CRLF
ahmed_sa 28-May-21 10:25am     CRLF
yes this is good public IActionResult Upload([FromBody] int selectedOwnerLevel) and after that how to access value of selectedOwnerLevel meaning how to get value 1 from key selectedOwnerLevel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900