Upload image file in entity framework database
How to insert,update and delete image in MVC using Entity framework database.Help me with details code please!
1 Answer
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.
Prakash JoshiRank #1,697 · 887 pointsPosted Feb 24, 2015, 3:47 AM EST
create a model like this
public class AdminDetailsViewModel
create a class like ImageRepository whic uses DbContext class
public class ImageRepository { private readonly ERPDbContext db = new ERPDbContext(); public int UploadImageInDatabase(HttpPostedFileBase file, AdminDetailsViewModel viewmodel) { viewmodel.AdminImage = ConvertToByte(file); var model = new AdminDetailsModel { AdminName = viewmodel.AdminName, AdminImage = viewmodel.AdminImage, }; db.AdminDetails.Add(model); int i = db.SaveChanges(); if (i == 1) { return 1; } else { return 0; } } private byte[] ConvertToByte(HttpPostedFileBase file) { byte[] imageByte = null; BinaryReader rdr = new BinaryReader(file.InputStream); imageByte = rdr.ReadBytes((int)file.ContentLength); return imageByte; } }create your controller like this
Its not full code but i think u get some idea