C# ASP.NET MVC中的HttpPostedFileBase返回NULL
HelpC# ASP.NET MVC中的HttpPostedFileBase返回NULL,c#,asp.net-mvc,C#,Asp.net Mvc,HelpHttpPostedFileBase返回NULL,我已经尝试了一些方法,并且在几个论坛上搜索过,但效果不错 这是我的密码 @using (Html.BeginForm("NuevoProveedor", "Account", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <
HttpPostedFileBase
返回NULL
,我已经尝试了一些方法,并且在几个论坛上搜索过,但效果不错
这是我的密码
@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>
}
您可以上传文件并将其url保存在数据库表中,如下所示: 查看:
@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>
...
}
[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");
}
为什么要放置两个嵌套表单,一个就足够了。嵌套表单是无效的html。
[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");
}