How can i upload & Save image in Ms-sql server-2008 database. Using MVC create view and I using EntityFarmework Model First.
Zakir
MVC2 & MVC4
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hanook159 reputationPosted Oct 31, 2013, 1:24 AM EDT
http://stackoverflow.com/questions/14583938/how-to-store-an-image-in-database-and-then-display-it-in-mvc
http://www.codeproject.com/Articles/658522/Storing-Images-in-SQL-Server-using-EF-and-ASP-NET
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 31, 2013, 1:40 AM EDT
Create a property in your respective viewmodel/model but not in EF model.
public HttpPostedFileBase PhotoUpload { get; set; }
Then add a input field in form,
@Html.TextBoxFor(m => m.PhotoUpload, new { type = "file" })
Then, in the controller action,
[HTTPPost]
public ActionResult SaveProfile(ProfileViewModel vm)
{
if (vm.PhotoUpload.ContentLength > 0) {
// A file was uploaded
var fileName = Path.GetFileName(vm.PhotoUpload.FileName);
var path = Path.Combine(Server.MapPath(uploadPath), fileName);
vm.PhotoUpload.SaveAs(path);
vm.Photo = uploadPath + fileName;
}
}