I have to save a PDF in a field of a table in SQL Server 2016.
By informing me I have seen that the best way to do this is to convert it to byte []. The problem is that I do not finish to find a method that works for me, since I want to do it in the following way:
When I create a new record, from the Create view, I have a field that is to select the file of my computer that I want to save in the DB.
This is the code:
@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>
}
What I want to know is how can I convert the file to byte [] when I select it in the Create view. And then how to convert it again (without having to download it) to display it in the Details view.
The code:
<div class="col-md-4">
<section>
<iframe src="~/src/donquijote1.pdf" style="width:650px; height:700px;" frameborder="0" />
</section>
</div>
Create statement:
[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);
}
The try / catch is because I had another problem and needed to expand the bug report