Edit Image in database when field is empty

Asked

Viewed 125 times

1

I have a code that uploads image. In the database worked beautifully. Now I need to make a code that changes the image only when it is selected. That is, in my case, when I change a record, you must select the image field. Otherwise, it is without image. Follows the code of Controller, Action Edit:

    public ActionResult Edit(Funcionario funcionario)
    {

        if (ModelState.IsValid)
        {
            if (funcionario.File == null)
            {

                funcionario.Foto = funcionario.Foto;
                funcionario.Freguesia = funcionario.Freguesia;
                funcionario.Concelho = funcionario.Concelho;

                db.Entry(funcionario).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {

                byte[] data = new byte[funcionario.File.ContentLength];
                funcionario.File.InputStream.Read(data, 0, funcionario.File.ContentLength);
                funcionario.Foto = data;

                db.Entry(funcionario).State = EntityState.Modified;
                db.SaveChanges();

            }

            return RedirectToAction("Index");
        }

        return View(funcionario);
    }

Follows View Create:

<td>@Html.LabelFor(model => model.Foto)</td>
        <td>
            @Html.TextBoxFor(model => model.File, new { Type="file"})
            @Html.ValidationMessageFor(model => model.Foto)

        </td>

.....

  • Could you clarify a little more? What is this field you need to select? Explain a little more

  • My problem is in this section there if I do not select a new image to edit that it recoils the image that is in the bd: if (funcio.File == null) funcionario. Photo = staff.Photo;

2 answers

1

There’s a lot of things wrong here:

funcionario.Foto = funcionario.Foto;
funcionario.Freguesia = funcionario.Freguesia;
funcionario.Concelho = funcionario.Concelho;

This assignment doesn’t make any sense.

All your code can be changed to the following:

[HttpPost]
public ActionResult Edit(Funcionario funcionario)
{
    if (ModelState.IsValid)
    {
        if (funcionario.File != null && funcionario.File.ContentLength > 0)
        {
            byte[] data = new byte[funcionario.File.ContentLength];
            funcionario.File.InputStream.Read(data, 0, funcionario.File.ContentLength);
            funcionario.Foto = data;
        }

        db.Entry(funcionario).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(funcionario);
}

Just setting your photo as an empty byte array is enough.

  • Thank you very much it helped me in the organization and logic. But what I want there (work.Photo = new byte[0];) is not to put empty but to leave what was already there or to do nothing.

  • @Evandrobarros I updated the answer.

  • Thank you but it didn’t work

  • For more information The photo attribute is byte (public byte[] Photo { get; set; })

0

Thank you all, some suggestion?

[HttpPost]
    [Authorize(Roles = "SuperAdmin, Admin")]
    public ActionResult Edit(Funcionario funcionario)
    {
        if (ModelState.IsValid)
        {
            if (funcionario.File != null && funcionario.File.ContentLength > 0)
            {
                byte[] data = new byte[funcionario.File.ContentLength];
                funcionario.File.InputStream.Read(data, 0, funcionario.File.ContentLength);
                funcionario.Foto = data;

                db.Entry(funcionario).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {

                db.Entry(funcionario).State = EntityState.Modified;
                db.Entry(funcionario).Property(m => m.Foto).IsModified = false;
                db.SaveChanges();
            }


            return RedirectToAction("Index");
        }

        return View(funcionario);
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.