locked
How to post pictures to controller RRS feed

  • Question

  • User-113329956 posted

    I save pictures as byte[] in the database. Im familiar with the HttpPostedFileBase  in asp.net but they got rid of that in .net Core 5 apparently How can I send the uploaded picture to the controller which converts it into picture and inserts into the database?

    <input name="UploadedPic" type="file"readonly />
    <button type="submit">Submit</button>
    

    C# controller

    public IActionResult UploadPictures()
        {
    
            return null;
    
        }

    Friday, February 12, 2021 3:56 AM

All replies

  • User-2006244509 posted

    You have to use IFormFile Interface of Asp.Net Core 5.0 

    You can define a DTO class with an IFormFile property, and use in combination with form having enctype - multipart/form-data

    public class UploadModel
    {
       public IFormFile UploadedPic { get; set; }
    }
    <form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
       <input asp-for="UploadedPic" />
       <button type="submit">Submit</button>
    </form>
    [HttpPost]         
    public IActionResult Index(UploadModel model)         
    {              
         // Here you can read the posted file stream from the request or copy it...          
    var stream = model.UploadedPic.OpenReadStream();               return View();         }

    Friday, February 12, 2021 6:44 AM