HttpPostedFileBase在提交后返回null
本文关键字:返回 null 提交 HttpPostedFileBase | 更新日期: 2023-09-27 18:24:44
当我尝试上传文件时,HttpPosterFileBase一直为null:
我有这样的代码在我的看法:
@using (Html.BeginForm("Import", "Control", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="fileUpload"/>
<input type="submit" value="Import" id="btnImport" class="button" />
}
这个代码和我的控制器:
[HttpPost]
public ActionResult Import()
{
HttpPostedFileBase file = Request.Files[fileUpload];
Other codes...
}
我也在我的控制器中尝试过这个:
[HttpPost]
public ActionResult Import(HttpPostedFileBase fileUpload)
{
Other codes...
}
按下提交按钮后,"文件"的值为null。
默认的模型绑定器按文件名绑定。您的输入名称为fileUpload
。。您的参数名称为file
。让它们保持不变是可行的。
您没有正确进行绑定,请更改以下内容:
[HttpPost]
public ActionResult Import(HttpPostedFileBase file)
{
// other stuff
}
收件人:
[HttpPost]
public ActionResult Import(HttpPostedFileBase fileUpload)
{
// other stuff
}
你的名字不匹配,因为下面的代码对我有效,除非你使用某种jQuery或Ajax来阻止它的工作,否则你应该很好。它是文件上传输入的名称和必须匹配的HttpPostedFileBase
名称。
@using (Html.BeginForm("import", "control", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="fileupload" />
<input type="submit" value="Import" id="btnImport" class="button" />
}
[HttpPost]
public ActionResult Import(HttpPostedFileBase fileupload)
{
return View();
}
感谢大家的回答。我相信所有这些答案都是正确的,但在我注意到我用页面嵌套了表单后,我能够解决这个问题。在阅读这里的答案后找到了解决方案:MVC。HttpPostedFileBase始终为空
再次感谢!干杯!:)