Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi how can i get the value of the input file username from view to Controller?
Heres my code:

HTML
<pre>  <label class="w-100">Username</label>
                        <input id="empUsername" class="input-form w-100" disabled>


And here is my Controller code:

ASP.NET
[HttpPost]
      public IActionResult OnPostMyUploader(IFormFile MyUploader)
      {
          // No point loading and resizing the image if we're not going to use it:
          if (MyUploader is null) return new ObjectResult(new { status = "fail" });

          string uploadsFolder = @"\\10.10.10.67\AQSImages\IdPictures\";
          string userfileName = (I WANT TO USE THE VALUE HERE TO STORE IN THIS VARIABLE) //empUsername
          string filePath = Path.Combine(uploadsFolder, userfileName);
          using var fileStream = new FileStream(filePath, FileMode.Create);

          using var image = System.Drawing.Image.FromStream(MyUploader.OpenReadStream());
          using var resized = new Bitmap(image, new System.Drawing.Size(150, 150));
          using var imageStream = new MemoryStream();
          resized.Save(imageStream, ImageFormat.Jpeg);


          // Reset the current position of the stream:
          imageStream.Seek(0L, SeekOrigin.Begin);
          imageStream.CopyTo(fileStream);
          return new ObjectResult(new { status = "success" });
      }


How can i get the value?

What I have tried:

Pass (Send) TextBox value from View to Controller without Model in ASP.Net MVC[^]
Posted
Updated 5-Jul-22 17:21pm

1 solution

create a public function in the controller to receive the data and send it with JQuery.

JavaScript
$.post('/Controller/Myfunction', {NamedVar: ViewVar1, NamedVar2: ViewVar2 }
     function(){
});

In the contoller:
C#
public void Myfunction(type NamedVar1, type NamedVar2)
{
   // use NamedVars as needed.
}
 
Share this answer
 
v2

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