asp.net-mvc - ASP.NET MVC Razor - 上传文件,HttpPostedFileBase 始终为 null

标签 asp.net-mvc jquery-mobile razor httppostedfilebase enctype

嗯,这很奇怪。 我正在做我在网上大量发现的所有事情,但仍然无法正常工作。 我想上传一个文件,所以在我放置的 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/

相关文章:

javascript - ASP.NET ScriptBundle 不适用于 .min.js?

c# - ASP.net MVC 异步执行

javascript - 将 jQuery Mobile (1.1.0) 与 AMD (RequireJS) 分开

blackberry - 运行 blackberry webworks 项目时,.cod 数据部分变大

asp.net-mvc-3 - 在Html.BeginForm()中使用DELETE表单方法吗?

cshtml @Url.Action(Action, Controller) 的 JavaScript 类比

razor - 为什么 dotnet 命令行构建在 MVC View 错误时失败,而 VS2017 没有?

asp.net-mvc - 人们在具有 ASP.NET/ASP.NET MVC 的云环境中使用哪些数据库?

jquery - PageInit Jquery 移动设备不工作

c# - Office 365个人资料头像图片