【发布时间】:2018-06-09 16:16:21
【问题描述】:
我已经尽力了,但是HttpPostedFileBase filee 始终是null
控制器动作
public ActionResult UploadFile(HttpPostedFileBase filee)
{
try
{
if (filee.ContentLength > 0)
{
string _FileName = Path.GetFileName(filee.FileName);
string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
filee.SaveAs(_path);
}
ViewBag.Message = "File Uploaded Successfully!!";
return View();
}
catch
{
ViewBag.Message = "File upload failed!!";
return View();
}
}
剃刀视图
@{
ViewBag.Title = "UploadFile";
}
<h2>UploadFile</h2>
@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
@Html.TextBox("file", "", new { type = "file" }) <br />
<input type="submit" value="Upload" />
@ViewBag.Message
</div>
}
【问题讨论】:
-
因为输入的文件名是
file而不是filee。他们需要匹配。 -
有什么好的理由吗?为了这。我们总是可以根据自己的意愿选择命名实例。雇员雇员=新雇员();
-
如果您要否决我的问题,请提及改进。一个人知道什么是错的还不够吗?只需对问题投反对票即可。
-
@StephenMuecke 感谢您的帮助,非常感谢。
标签: c# asp.net-mvc razor file-upload