【问题标题】:HttpPostedFileBase always return null in ASP.NET MVCHttpPostedFileBase 在 ASP.NET MVC 中总是返回 null
【发布时间】:2012-01-23 00:06:41
【问题描述】:

我在 ASP.NET MVC 中上传文件时遇到问题。 我的代码如下:

查看:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index2</h2>
@using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <input type="file" />
  <input type="submit" />
}

控制器:

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
    if (uploadFile != null && uploadFile.ContentLength > 0)
    {
        string filePath = Path.Combine(Server.MapPath("/Temp"), Path.GetFileName(uploadFile.FileName));
        uploadFile.SaveAs(filePath);
    }
    return View();
}

但是uploadFile 总是返回null。 谁能知道为什么??

【问题讨论】:

    标签: asp.net-mvc file-upload


    【解决方案1】:
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Index2</h2>
    @using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
      <input type="file" name="uploadFile"/>
      <input type="submit" />
    }
    

    您必须提供 输入类型文件的名称到 uploadFile,以便在 ASP.net mvc 中建模绑定工作,并且
    还要确保输入类型文件的名称和HttpPostedFileBase 的参数名称相同。

    【讨论】:

    • 我没有错过字段名称,但由于缺少表单定义中的 enctype 参数而出现了相同的“空”问题。谢谢你的例子。
    • 并且控制器参数中的名称必须与表单中的名称一致。
    • 谢谢 - 我正在上传多个文件(所以我的 FileUpload 方法的参数类型为 IEnumerable),我将所有文件输入元素命名为相同以匹配参数名称它就像一个魅力。
    • 我错过了 method="POST": &lt;form id="enviarDocumento" enctype="multipart/form-data" action="@Url.Action("AddDocument", "Document")" method="POST"&gt;
    • @Jonathan wood :它不是 ASP.net mvc 但它与 html 有关。 w3.org/TR/html401/interact/forms.html#h-17.13.4.2
    【解决方案2】:

    我已经尝试了大多数在线发布的关于该主题的解决方案,但发现最好使用解决方法。

    我做了什么并不重要,HttpPostedFileBase 和/或 HttpPostedFile 始终为空。使用 HttpContext.Request.Files 集合似乎一点也不麻烦。

    例如

     if (HttpContext.Request.Files.AllKeys.Any())
            {
                // Get the uploaded image from the Files collection
                var httpPostedFile = HttpContext.Request.Files[0];
    
                if (httpPostedFile != null)
                {
                    // Validate the uploaded image(optional)
    
                    // Get the complete file path
                    var fileSavePath =(HttpContext.Server.MapPath("~/UploadedFiles") + httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf(@"\")));
    
                    // Save the uploaded file to "UploadedFiles" folder
                    httpPostedFile.SaveAs(fileSavePath);
                }
            }
    

    在上面的例子中,我只抓取了第一个文件,但这只是一个循环遍历集合以保存所有文件的问题。

    HTH

    罗伯

    【讨论】:

    • 这行得通!无法确定为什么使用其他技术它总是为空。谢谢!
    • 谢谢你,你太棒了!我花了两天时间才找到解决方案,@roblem
    • “我做了什么真的没关系,HttpPostedFileBase 和/或 HttpPostedFile 始终为空”......完全正确。如上所述工作。
    【解决方案3】:

    在我的场景中,问题出在 id 属性上,我遇到了这个问题:

    <input type="file" name="file1" id="file1" />
    

    灵魂是删除id:

    <input type="file" name="file1"  />
    

    【讨论】:

    • 该死,就我而言,我使用的是 HttpPostedFile 而不是 HttpPostedFileBase.. 程序员错误:/
    • 塞缪尔你拯救了我的一天!虽然在stackoverflow.com/a/24911221/2346618 有解释,但我认为在签名中使用基类 HttpPostedFileBase 是愚蠢的。在面向对象编程中,基类用于帮助构建具体类,接口用于协议。使用 IHttpPostedFile 之类的接口将减少 IMO 出错的可能性。
    【解决方案4】:

    虽然不是这个特定用户的答案,但我想指出HTML requires that the form tag has an enctype attribute with the value multipart/form-data. 当然属性和它的值都必须是正确的。

    对于mvc,这意味着在使用beginform时,应该使用带有htmlAttributes参数的版本

    【讨论】:

    【解决方案5】:

    也可能有另一种情况。就我而言,我遇到了这个问题,因为我直接在我的 MVC 视图中渲染脚本标签,而 IE 在那里给出了问题。

    视图中正确的代码如下:

    @section scripts
    {
        <script>
            $(document).ready(function () {
                $('.fileinput').fileinput();
    ...
    }
    

    【讨论】:

      【解决方案6】:

      您需要使用 Razor 代码来设置输入文件的名称。

      <input type="file" name="@Html.Namefor(m => m.propertyName)">
      

      【讨论】:

        猜你喜欢
        • 2016-04-08
        • 2019-09-17
        • 2014-06-29
        • 2017-09-24
        • 1970-01-01
        • 2019-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多