我正在尝试将xml文件上传到我的网站。但是,无论我尝试上传哪个文件,我的代码中的HttpPostedFileBase元素都是null。我不明白为什么会这样。我已经按照我上传文件时可以找到的所有示例进行操作,但似乎没有任何意义。这是控制器方法
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase xmlFile)
{
if (xmlFile != null && xmlFile.ContentLength > 0)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile.InputStream);
// other logic later
return RedirectToAction("Index");
}
return RedirectToAction("UploadFailed");
}
和cshtml:
@{
ViewBag.Title = "Upload";
}
@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="OK" />
}
答案 0 :(得分:2)
名字错误。 action参数名为xmlFile
,而文件输入名为file
。您需要在命名约定中保持一致:
<input type="file" name="xmlFile" />
我还邀请你查看Phil Haack关于这个主题的blog post。