I`m trying to retrieve image bytes from my database. I use entity framework code first. I pass in the Httppost method 2 parameters, one is the modelview and the second one is the HttpPostedFileBase as list. When i debug the method i can see that there are files in the list and they also have ContentLength 123233. Then i transform this into bytes in order to store in in the database.
However, when i store it into the database the file name and the related jobpost id is inserted corectly but the bytes for the images are 0x00000...infinite 0.
When i parse them into the view i have no image. why my code doesn`t save bytes but only 00000...?
here is my code.
[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();
}
the model
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; }
}
view
@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>
model.
public class ManageAccountCombinedViewModel
{
public List<MyPostedJobsViewModel> MyPostedJobsViewModel { get; set; }
}
model
public class MyPostedJobsViewModel
{
public string Headline { get; set; }
public DateTime PostedDate { get; set; }
public List<JobPicturesViewModel> JobPictures { get; set; }
}
model
public class JobPicturesViewModel
{
public byte[] JobImageContentBytes { get; set; }
}
You did not read content of file to byte[]
, change the code to read content to this
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);
}
}