Help HttpPostedFileBase
returns NULL
, I have already tried several things and I have searched in several forums but it works.
This is my code
@using (Html.BeginForm("NuevoProveedor", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<center>
<form method="post" enctype="multipart/form-data">
<input type="file" id="btnFile" name="file" />
<input type="submit" value="Save" id="btnSave" />
</form>
</center>
}
Controller
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult NuevoProveedor(HttpPostedFileBase file, SubirArchivoModelo model)
{
SubirArchivoModelo modelo = new SubirArchivoModelo();
if (file != null)
{
string Ruta = file.FileName;
return View();
}
else
{
ViewBag.error = "No se pudo generar la carpeta, comunicate con Soporte Tecnico";
return View("UsuarioNuevo");
}
}
you can upload file and save its url in the database table like this:
View:
@using(Html.BeginForm("NuevoProveedor", "Account",FormMethod.Post,new {enctype="multipart/form-data"}))
{
...
<div class="editor-field">
<input type="file" id="btnFile" name="file" />
<input type="submit" value="Save" id="btnSave" />
</div>
...
}
Action:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult NuevoProveedor(SubirArchivoModelo model)
{
if (ModelState.IsValid)
{
if(Request.Files.Count > 0)
{
HttpPostedFileBase file = Request.Files[0];
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
model.FileLocation = Path.Combine(
Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(model.FileLocation);
}
//db.TableEntity.Add(model);
//db.SaveChanges();
//return RedirectToAction("Index");
}
}
return View("UsuarioNuevo");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.