An image is nothing more than an array of bytes, so Entity Framework makes it that type of property. What you need to do to save the image that the user is trying to upload is to convert your input / file field to an array of bytes.
View
@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<label for="file">Upload Image:</label>
<input type="file" name="file" id="file" style="width: 100%;" />
<input type="submit" value="Upload" class="submit" />
}
Controller
public ActionResult FileUpload(HttpPostedFileBase file)
{
if (file != null)
{
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
var context = new Models.BDEntities();
context.activofijoes.Add(new Models.activofijo()
{
FotoAF = array,
});
context.SaveChanges();
}
}
return RedirectToAction("actionname", "controller");
}
Additionally, to show your image in the view, I recommend creating a controller that manages the arrangement of images and you return the image:
Controlller
public class ImagesController : Controller
{
public ActionResult Index(int id)
{
var context = new Models.DBEntities();
byte[] imageData = context.activofijoes.FirstOrDefault(i => i.id == id)?.FotoAF;
if (imageData != null)
{
return File(imageData, "image/png"); // Might need to adjust the content type based on your actual image type
}
return null;
}
}
View
<img src="@Url.Action("Index", "Images", new { id = 1 })" />