I have an MVC 5 with Entity Framework application, database first and I am new to MVC. I have a file picker control that selects an image and in Action Result class of the create view, I encode the picture for storage in SQL. I have the encoding correct, but the data from the view is in the Bind attributes and I want to update the property from the Bind with encoded picture before I write it to the database. When I try to write to the database, the whole app simply churns until it runs out of memory. Obviously, I am missing a piece.
View: This is the div with the filepicker and the place where the Entity Framework wanted the coded pic to go.
<div class="col-md-10"><br/><br/>
@Html.TextBox("beforeEncode", "", new { type = "file" })<br />
@Html.LabelFor(model => model.fldPhoto, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.fldPhoto, new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.fldPhoto, "", new { @class = "text-danger" })
</div>
</div>
Controller: This is the controller with the irrelevant parts deleted. I have a breakpoint set on the db.SaveChanges(); line for checking if there was an issue with the encoding, but even with it removed, it just churns
public ActionResult Create([Bind(Include = "fldPhotoID,fldPhoto,beforeEncode")] tblPhoto tblPhoto, String beforeEncode)
{
var fileupload = beforeEncode;
try
{
if (ModelState.IsValid)
{
if (fileupload != null)
{
FileStream fs;
fs = new FileStream(beforeEncode, FileMode.Open, FileAccess.Read);
byte[] PicEncoded = new byte[fs.Length];
fs.Read(PicEncoded, 0, Convert.ToInt32(fs.Length));
fs.Close();
tblPhoto.fldPhoto = PicEncoded;
}
db.tblPhotoes.Add(tblPhoto);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}