嗯,这很奇怪。 我正在做我在网上大量发现的所有事情,但仍然无法正常工作。 我想上传一个文件,所以在我放置的 View 中:
@using (Html.BeginForm("Import", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="uploadFile" />
<input type="submit" value="Importa" />
}
这是主页/导入:
[AcceptVerbs(HttpVerbs.Post)] // or [HttpPost], nothing changes
public ActionResult Import(HttpPostedFileBase uploadFile)
{
Debug.WriteLine("Inside Import");
if (uploadFile == null)
Debug.WriteLine("null");
// Verify that the user selected a file
if (uploadFile != null && uploadFile.ContentLength > 0)
{
Debug.WriteLine("Valid File");
...
}
}
它总是打印Inside Import + null。所以我确定我调用了正确的 Controller 操作,但它总是收到一个空的 HttpPostedFileBase。
有没有人对此有建议或已经发现(并解决)了这种问题? 谢谢!
最佳答案
发现问题。您必须将 html 属性“@data_ajax = “false””放在 View 中靠近 enctype 的位置。 所以,如果你使用的是 jQuery Mobile,一定要这样写
@using (Html.BeginForm("Import", "Home", FormMethod.Post, new { enctype = "multipart/form-data", @data_ajax = "false" }))
希望这会对某人有所帮助!
关于asp.net-mvc - ASP.NET MVC Razor - 上传文件,HttpPostedFileBase 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13292173/