tengo que guardar un PDF en un campo de una tabla en SQL Server 2016.
Informándome he visto que la mejor forma de hacerlo es convirtiéndolo en byte[]. El problema es que no termino de dar con un método que me sirva, puesto que lo quiero hacer de la siguiente manera:
Cuando creo un nuevo registro, desde la vista Create, tengo un campo que es para seleccionar el fichero de mi equipo que quiero guardar en la BBDD.
Este es el código:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Compras</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
</div> <div class="form-group">
@Html.LabelFor(model => model.empresaC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.empresaC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.empresaC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.num_diarC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.num_diarC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.num_diarC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.num_movC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.num_movC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.num_movC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.mes_diarC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.mes_diarC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.mes_diarC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.anno_diarC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.anno_diarC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.anno_diarC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.codigoC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.codigoC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.codigoC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.nombreC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.nombreC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.nombreC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.num_factC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.num_factC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.num_factC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.fec_factC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.fec_factC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fec_factC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.imp_factC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.imp_factC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.imp_factC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.tiva_factC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.tiva_factC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.tiva_factC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.kg_facC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.kg_facC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.kg_facC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.obs_factC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.obs_factC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.obs_factC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.imagenC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4 btn">
@Html.TextBoxFor(model => model.imagenC, new { type="file"})
@Html.ValidationMessageFor(model => model.imagenC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Añadir" class="btn btn-default" />
</div>
</div>
</div>
}
Lo que quiero es saber cómo puedo convertir el fichero en byte[] al seleccionarlo en la vista Create. Y después como volverlo a convertir (sin tener que descargarlo) para visualizarlo en la vista Details.
El código:
<div class="col-md-4">
<section>
<iframe src="~/src/donquijote1.pdf" style="width:650px; height:700px;" frameborder="0" />
</section>
</div>
Sentencia Create:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "idC,empresaC,num_diarC,num_movC,mes_diarC,anno_diarC,codigoC,nombreC,num_factC,fec_factC,imp_factC,tiva_factC,kg_facC,obs_factC,id_imagenC,imagenC")] ModelCompra compra)
{
Compra dbCompra = new Compra();
if (ModelState.IsValid)
{
dbCompra = new ModelCompra().getDbCompra(compra);
db.Compras.Add(dbCompra);
try
{
db.SaveChanges();
}
catch (DbEntityValidationException ex)
{
foreach (DbEntityValidationResult item in ex.EntityValidationErrors)
{
// Get entry
DbEntityEntry entry = item.Entry;
string entityTypeName = entry.Entity.GetType().Name;
// Display or log error messages
foreach (DbValidationError subItem in item.ValidationErrors)
{
string message = string.Format("Error '{0}' occurred in {1} at {2}",
subItem.ErrorMessage, entityTypeName, subItem.PropertyName);
throw new DbEntityValidationException(message);
}
}
}
return RedirectToAction("Index");
}
return View(compra);
}
El try/catch está porque tuve otro problema y necesitaba ampliar el informe de errores