ASP.NET Core storing image to SQL Server null

I am a beginner in ASP.NET Core 3.1 and i am creating this application with EF Core tutorial from Microsoft site, when i try to create a new Job and store Photo, Photo attribute says null on database, i don’t know why?
I used debugger to see if image is coming to Create method in controller and IFormFile Image parameter says it is null.

Model:

        public int JobID { get; set; }

        [Required]
        [Column("Title")]
        [StringLength(150, MinimumLength = 3)]
        [Display(Name = "Title")]
        public string Title { get; set; }

        [Display(Name = "Photo")]
        public byte[] Photo { get; set; }

        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
        [Display(Name = "Date")]
        public DateTime Date { get; set; }

        [Required]
        [Column("CategoryID")]
        [Display(Name = "CategoryID")]
        public int CategoryID { get; set; }
        
        [Required]
        [Column("CompanyID")]
        [Display(Name = "CompanyID")]
        public int CompanyID { get; set; }

        public Category Category { get; set; }
        public Company Company { get; set; }
        public ICollection<Application> Applications { get; set; }

View – Create.html:
<form asp-action="Create" method="post" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Title" class="control-label"></label>
                <input asp-for="Title" class="form-control" />
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Photo" class="control-label"></label>
                <input type="file" name="Photo" class="form-control" />
                @*<span asp-validation-for="Photo" class="text-danger"></span>*@
            </div>
            <div class="form-group">
                <label asp-for="Date" class="control-label"></label>
                <input asp-for="Date" class="form-control" />
                <span asp-validation-for="Date" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="CategoryID" class="control-label"></label>
                <select asp-for="CategoryID" class ="form-control" asp-items="ViewBag.CategoryID"></select>
            </div>
            <div class="form-group">
                <label asp-for="CompanyID" class="control-label"></label>
                <select asp-for="CompanyID" class ="form-control" asp-items="ViewBag.CompanyID"></select>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>

Controller method JobsController:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("JobID,Title,Photo,Date,CategoryID,CompanyID")] Job job, IFormFile Image)
{
    if (ModelState.IsValid)
    {
        if (Image != null) {
            if (Image.Length > 0) {
                byte[] p1 = null;
                using (var fs1 = Image.OpenReadStream())
                using (var ms1 = new MemoryStream())
                {
                    fs1.CopyTo(ms1);
                    p1 = ms1.ToArray();
                }
                job.Photo = p1;
            }
        }
        _context.Add(job);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    ViewData["CategoryID"] = new SelectList(_context.Categories, "CategoryID", "CategoryID", job.CategoryID);
    ViewData["CompanyID"] = new SelectList(_context.Companies, "CompanyID", "Name", job.CompanyID);
    return View(job);
}

This is where it says null: IFormFile Image null, Job Photo attribute also null
public async Task<IActionResult> Create([Bind("JobID,Title,Photo,Date,CategoryID,CompanyID")] Job job, IFormFile Image)

Other attributes are stored fine, i tried almost all similar solutions but none of them helped me. Any idea?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can try this:

     var file = HttpContext.Request.Form.Files["Photo"];
     if (file!=null && file.Length > 0)
       {
           using (var ms = new MemoryStream())
           {
                file.CopyTo(ms);
                var fileBytes = ms.ToArray();
                // store your bytes in db ...    
           }
       }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x