Error saving EF ASP.NET image

0

Currently I have the following structure in my model for an image field of my bd

 [Table("especialidad")]
public class clsEspecialidad
{

    private int iId_Especialidad;

    [Key]
    [Column("id_especialidad")]
    [Display(Name = "Id Especialidad")]

    public int Id_Especialidad
    {
        get { return iId_Especialidad; }
        set { iId_Especialidad = value; }
    }


    private string iEspecialidad;

    [Column("especialidad")]
    [Required]
    [MinLength(6, ErrorMessage = "Campo Especialidad Min 6 Caracteres")]
    [MaxLength(120, ErrorMessage = "Campo Cargo Max 120 Caracteres")]
    [Display(Name = "Especialidad ")]
    [DataType(DataType.Text)]
    public string Especialidad
    {
        get { return iEspecialidad; }
        set { iEspecialidad = value; }
    }


    private string iDescripcion;

    [Column("descripcion")]
    [Required]
    [MinLength(6, ErrorMessage = "Campo Descripción Min 10 Caracteres")]
    [MaxLength(120, ErrorMessage = "Campo Descripción Max 200 Caracteres")]
    [Display(Name = "Descripción ")]
    [DataType(DataType.MultilineText)]
    public string Descripcion
    {
        get { return iDescripcion; }
        set { iDescripcion = value; }
    }



    private byte[] iImagen;

    [Column("imagen")]
    [Required]
    [Display(Name = "Imagen ")]
    [DataType(DataType.Upload)]
    public byte[] Imagen
    {

        get {
            return iImagen;
        }
        set {
            iImagen = value;
        }
    }

    private bool iEstado;

    [Column("estado")]
    [Required]
    public bool Estado
    {
        get { return iEstado; }
        set { iEstado = value; }
    }

}

When Executing the system it does not show errors, but when trying to register an image to the bd the Exception skips me

  

System.FormatException: The entry is not a valid Base 64 string because it contains a non-Base 64 character, more than two fill characters, or an invalid character between the fill characters.

My Controller

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id_Especialidad,Especialidad,Descripcion,Imagen,Estado")] clsEspecialidad clsEspecialidad)
    {
        if (ModelState.IsValid)
        {
            db.clsEspecialidades.Add(clsEspecialidad);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(clsEspecialidad);
    }

And in my view I have an input type file for my image

@using (Html.BeginForm("Create", "Especialidad", FormMethod.Post,
                                              new { enctype = "multipart/form-data" })){ 

 @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>clsEspecialidad</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Especialidad, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Especialidad, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Especialidad, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Descripcion, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Descripcion, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Descripcion, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Imagen, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @*@Html.EditorFor(model => model.Imagen, new { htmlAttributes = new { @class = "form-control" } })*@
            @*@Html.ValidationMessageFor(model => model.Imagen, "", new { @class = "text-danger" })*@
            <input type="file" name="Imagen" value="" id="Imagen">
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Estado, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.Estado)
                @Html.ValidationMessageFor(model => model.Estado, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

}

I understand that is why I need to convert the obtained image to bytes, how can I do this and where would it be a model? , controller?.

    
asked by DarkFenix 11.05.2017 в 21:18
source

1 answer

0
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id_Especialidad,Especialidad,Descripcion,Imagen,Estado")] clsEspecialidad clsEspecialidad, HttpPostedFileBase upload)
{
    if (ModelState.IsValid)
    {
      if (upload != null && upload.ContentLength > 0)
      {           
        using (var reader = new System.IO.BinaryReader(upload.InputStream))
        {
          var contenidoFoto = reader.ReadBytes(upload.ContentLength);
          clsEspecialidad.Imagen= contenidoFoto;
        }              
      }
        db.clsEspecialidades.Add(clsEspecialidad);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(clsEspecialidad);
}
    
answered by 12.05.2017 в 00:10