0

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");
            }
Steve Greene
  • 12,029
  • 1
  • 33
  • 54
Ralph
  • 11
  • 1
  • why are you not binding to HttpPostedFileBase? look at this for sample code: https://stackoverflow.com/questions/25125127/asp-net-mvc-4-c-sharp-httppostedfilebase-how-do-i-store-file – jle Aug 14 '17 at 20:53
  • Change `String beforeEncode` to `HttpPostedFileBase beforeEncode` so you correctly bind your file input. –  Aug 14 '17 at 22:16
  • That did it. It is posting to the DB – Ralph Aug 15 '17 at 19:32

0 Answers0