Inquiridor
Upload de arquivos salvando caminho no baco.

Pergunta
-
Quero fazer o upload de arquivo e salvar o endereço no banco.
View:
@model TexMvc.Models.PacotePdf
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "PacotePdf", FormMethod.Post, new { enctype = "multpart/forme-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="form-horizontal">
<h4>PacotePdf</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.NomePacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NomePacotePdf)
@Html.ValidationMessageFor(model => model.NomePacotePdf)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PaisPacotePdf, "PaisPacotePdf", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("PaisPacotePdf", String.Empty)
@Html.ValidationMessageFor(model => model.PaisPacotePdf)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ImgPacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.ImgPacotePdf)
@Html.ValidationMessageFor(model => model.ImgPacotePdf)*@
<input type="file" id="ImgPacotePdf" name="arquivo" />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PdfPacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.PdfPacotePdf)
@Html.ValidationMessageFor(model => model.PdfPacotePdf)*@
<input type="file" id="PdfPacotePdf" name="arquivo" />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.VisivelPacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.VisivelPacotePdf)*@
@Html.RadioButtonFor(model => model.VisivelPacotePdf, "true", true) Visivel
@Html.RadioButtonFor(model => model.VisivelPacotePdf, "false", false) "Não diponiver"
@Html.ValidationMessageFor(model => model.VisivelPacotePdf)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DestaquePacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.DestaquePacotePdf)*@
@Html.RadioButtonFor(model => model.DestaquePacotePdf, "true", true) Destaque
@Html.RadioButtonFor(model => model.DestaquePacotePdf, "false", false) "Não diponiver"
@Html.ValidationMessageFor(model => model.DestaquePacotePdf)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BloqueioPacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.BloqueioPacotePdf)*@
@Html.RadioButtonFor(model => model.BloqueioPacotePdf, "true", true) Bloqueio
@Html.RadioButtonFor(model => model.BloqueioPacotePdf, "false", false) "Não diponiver"
@Html.ValidationMessageFor(model => model.BloqueioPacotePdf)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ConradPacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.ConradPacotePdf)*@
@Html.RadioButtonFor(model => model.ConradPacotePdf, "true", true) Conrad
@Html.RadioButtonFor(model => model.ConradPacotePdf, "false", false) "Não diponiver"
@Html.ValidationMessageFor(model => model.ConradPacotePdf)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ViniculaPacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.ViniculaPacotePdf)*@
@Html.RadioButtonFor(model => model.ViniculaPacotePdf, "true", true) Vinicula
@Html.RadioButtonFor(model => model.ViniculaPacotePdf, "false", false) "Não diponiver"
@Html.ValidationMessageFor(model => model.ViniculaPacotePdf)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BannerDestaquePacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.BannerDestaquePacotePdf)*@
@Html.RadioButtonFor(model => model.BannerDestaquePacotePdf, "true", true) Banner
@Html.RadioButtonFor(model => model.BannerDestaquePacotePdf, "false", false) "Não diponiver"
@Html.ValidationMessageFor(model => model.BannerDestaquePacotePdf)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ValidoPacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ValidoPacotePdf)
@Html.ValidationMessageFor(model => model.ValidoPacotePdf)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PrioridadePacotePdf, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PrioridadePacotePdf)
@Html.ValidationMessageFor(model => model.PrioridadePacotePdf)
</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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>---------------------------------------------------
Controle:
// GET: PacotePdf/Create
public ActionResult Create()
{
ViewBag.PaisPacotePdf = new SelectList(db.Pais, "PaisId", "NomePais");
return View();
}
// POST: PacotePdf/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "IdPacotePdf,NomePacotePdf,PaisPacotePdf,ImgPacotePdf,PdfPacotePdf,VisivelPacotePdf,DestaquePacotePdf,BloqueioPacotePdf,BannerDestaquePacotePdf,ValidoPacotePdf,PrioridadePacotePdf")] PacotePdf pacotePdf)
{
if (Request.Files.Count == 1)
{
HttpPostedFileBase ImgPacotePdf = Request.Files[0];
if (ImgPacotePdf.ContentLength > 0)
{
var uploadPath = Server.MapPath("~/img/Pacote");
// string caminhoArquivo = Path.Combine(@uploadPath, Path.GetFileName(ImgPacotePdf.FileName));
String caminhoArquivo = System.IO.Path.GetExtension(ImgPacotePdf.FileName).ToLower();
ViewBag.ImgPacotePdf = Path.GetFileName(ImgPacotePdf.FileName);
ImgPacotePdf.SaveAs(caminhoArquivo);
}
}
if (Request.Files.Count == 1)
{
HttpPostedFileBase PdfPacotePdf = Request.Files[0];
if (PdfPacotePdf.ContentLength > 0)
{
var uploadPath = Server.MapPath("~/pdf");
// string caminhoArquivo = Path.Combine(@uploadPath, Path.GetFileName(PdfPacotePdf.FileName));
String caminhoArquivo = System.IO.Path.GetExtension(PdfPacotePdf.FileName).ToLower();
ViewBag.PdfPacotePdf = Path.GetFileName(PdfPacotePdf.FileName);
PdfPacotePdf.SaveAs(caminhoArquivo);
}
}
pacotePdf.ImgPacotePdf = ("/img/Pacote/" + ViewBag.ImgPacotePdf);
pacotePdf.PdfPacotePdf = ("../pdf/" + ViewBag.PdfPacotePdf);
if (ModelState.IsValid)
{
db.PacotePdf.Add(pacotePdf);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.PaisPacotePdf = new SelectList(db.Pais, "PaisId", "NomePais", pacotePdf.PaisPacotePdf);
return View(pacotePdf);
}
paiva
Todas as Respostas
-
E qual a sua dúvida? Qual erro está tendo?
If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". Se achou este post útil, por favor clique em "Votar como útil". Se por um acaso respondeu sua dúvida, lembre de "Marcar como Resposta".