HttpPostedFileBase 在数据库中保存图像的问题

Problem in HttpPostedFileBase saving images in database

我正在尝试从我的数据库中检索图像字节。我首先使用 entity framework 代码。我传入了 Httppost 方法 2 个参数,一个是模型视图,第二个是 HttpPostedFileBase 作为列表。当我调试该方法时,我可以看到列表中有文件,它们也有 ContentLength 123233。然后我将其转换为字节以便存储在数据库中。

但是,当我将它存储到数据库中时,文件名和相关的职位 ID 被正确插入,但图像的字节是 0x00000...无限 0。

当我将它们解析到视图中时,我没有图像。为什么我的代码不保存字节而只保存 00000...?

这是我的代码。

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(PostJobViewModel JobModel, List<HttpPostedFileBase> jobImages)
    {

        if (User.Identity.Name == "")
        {
            return RedirectToAction("Login","Account");
        }
        else
        {

            var uId = (from u in _context.Users where u.Email == User.Identity.Name select u.Id).First();
            var isNull = true;
            var tmpIsNull = true;
            foreach (var item in jobImages)
            {

                if (item == null)
                {
                    isNull = true;
                }
                else
                {
                    isNull = false;
                    tmpIsNull = isNull;
                }
            }
            ApplicationUser userObj = _context.Users.Single(u => u.Id == uId);
            if (isNull && tmpIsNull == true)
            {

                if (ModelState.IsValid)
                {
                        var newJob = new JobPost
                        {
                            Category = JobModel.Category,
                            JobAddress = JobModel.JobAddress,
                            AboutJob = JobModel.AboutJob,
                            JobCity = JobModel.JobCity,
                            JobPostCode = JobModel.JobPostCode,
                            Headline = JobModel.Headline,
                            dateJobPosted=DateTime.Now,
                            UserId = uId,
                            User= userObj,
                            JobService = JobModel.JobService

                        };
                    _context.jobPosts.Add(newJob);
                    _context.SaveChanges();
                }

            }
            else
            {


                if (ModelState.IsValid)
                {


                    var newJob = new JobPost {
                        Category = JobModel.Category,
                        JobAddress = JobModel.JobAddress,
                        AboutJob = JobModel.AboutJob,
                        JobCity = JobModel.JobCity,
                        JobPostCode = JobModel.JobPostCode,
                        dateJobPosted = DateTime.Now,
                        UserId = uId,
                        Headline = JobModel.Headline,
                        User = userObj,
                        JobService=JobModel.JobService

                    };

                    _context.jobPosts.Add(newJob);    
                    _context.SaveChanges();

                    // Retrieve job post to add the images to the post

                    JobPost post = _context.jobPosts.Single(j => j.JobPostId == newJob.JobPostId);

                    foreach (var item in jobImages)
                    {
                        if (item!=null)
                        {
                            JobImage img = new JobImage
                                {
                                    JobFileName=item.FileName,
                                    JobImageContentBytes= new byte[item.ContentLength],
                                    JobPost = post
                                };
                            _context.jobImages.Add(img);
                        }


                    }

                    _context.SaveChanges();



                }
                else
                {
                    return View(JobModel);
                }
            }



        }








        var whatWorkToBeDone = new SelectList(_context.JobWizardCategories, "WhatToDoneId", "WhatToDoneItem");
        ViewBag.whatWorkToBeDone = whatWorkToBeDone;

        return View();
    }

模特

 public class PostJobViewModel
{

    [Required]
    public WorkCategory Category { get; set; }
    [Required]
    public string Headline { get; set; }
    [Required]
    public string JobAddress { get; set; }

    [Required]
    public string AboutJob { get; set; }
    [Required]
    public string JobCity { get; set; }
    [Required]
    public string JobPostCode { get; set; }

    public ServiceCategory JobService { get; set; }
}

查看

@model  FinalWorkFinder.Models.ManageAccountCombinedViewModel
 @{
   ViewBag.Title = "Dashboard";
 }

 <section class="my-posted-jobs">

<h1 class="my-posted-jobs-title">@ViewBag.myJobsTitle</h1>
<div class="border-job"></div>
<ul class="my-jobs-section">
    @foreach (var item in Model.MyPostedJobsViewModel)
    {
        <li class="separate-job">
            <div class="content-li">
                <h2 class="content-li-headline">@item.Headline</h2>
                <div class="pictures-li">
                    @{

                        foreach (var pic in item.JobPictures)
                        {
                            //base 64 img to screen
                            if (pic != null)
                            {
                                var base64 = 
       Convert.ToBase64String(pic.JobImageContentBytes);
                                var img = 
           string.Format("data:image/jpg;base64,{0}", base64);
                                <img class="posted-pic" src="@img" alt="" 
        />
                            }

                        }

                    }
                </div>
                <div class="job-date-li">@item.PostedDate</div>
            </div>
        </li>
    }
</ul>


         </section>

型号。

 public class ManageAccountCombinedViewModel
{
    public List<MyPostedJobsViewModel> MyPostedJobsViewModel { get; set; }

}

型号

public class MyPostedJobsViewModel
{
    public string Headline { get; set; }
    public DateTime PostedDate { get; set; }
    public List<JobPicturesViewModel> JobPictures { get; set; }
}

型号

 public class JobPicturesViewModel
{
    public byte[] JobImageContentBytes { get; set; }
}

您没有将文件内容读取到 byte[],更改代码以读取内容到此

foreach (var item in jobImages)
 {
        if (item!=null)
        {
           byte[] uploadedFile = new byte[item.InputStream.Length];
           item.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
           JobImage img = new JobImage
                          {
                              JobFileName=item.FileName,
                              JobImageContentBytes= uploadedFile,
                              JobPost = post
                          };
           _context.jobImages.Add(img);
         }
}