【发布时间】:2012-08-10 09:15:15
【问题描述】:
我不知道我缺少什么,但我需要使用 C# MVC 3 上传文件。我按照 SO 中的说明进行操作,但文件始终为空。
这是我的实际测试代码:
HTML
@using (Html.BeginForm("Prc", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
}
控制器
[HttpPost]
public ActionResult Prc(HttpPostedFile file)
{
if (file != null && file.ContentLength > 0)
{
var filename = System.IO.Path.GetFileName(file.FileName);
var path = System.IO.Path.Combine(Server.MapPath("~/Content/Images"), filename);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
当我运行 Web 应用程序时,我附加了一个文件,然后单击提交。但是当我到达Controller 时,file 对象是null。始终null。我尝试了一个XML 文件、一个JPEG 文件和一个GIF 文件,但它们都不起作用。
除了只这些代码之外,我还应该配置其他东西吗?
谢谢
【问题讨论】:
标签: asp.net-mvc-3 c#-4.0 file-upload asp.net-mvc-controller