【问题标题】:Request.Files.Count always 0 while uploading image in MVC 5在 MVC 5 中上传图像时,Request.Files.Count 始终为 0
【发布时间】:2020-09-17 02:33:07
【问题描述】:

我在一个模型上有一个 Post 控制器,其中包含一些字符串字段和一个图像。完全相同的代码适用于 MVC4,但在 MVC 5 中,Request.Files.Count 始终为 0

我的模型有 byte[] 用于图像而不是 HttpPostedFileBase

我的观点:

@using (Html.BeginForm("Create", "HotelManager", FormMethod.Post, new { enctype = "multipart/form-data", data_ajax = "false" }))
{
    @Html.AntiForgeryToken()
    @Html.TextBoxFor(model => model.Title, new { @class = "form-control" })
    @Html.TextAreaFor(model => model.Description, new { @class = "form-control", @rows = "7" })
    <input type="file" class="form-control filestyle">
    <input type="submit" value="Submit" class="btn btn-block" />
}

我试过省略 data_ajax = "false" 但没有用。

我的 Post Controller 如下:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Title,Description,Image")] Hotel hotel)
    {
        if (ModelState.IsValid)
        {
            // deal with the uploaded file
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    // Code to process image, resize, etc goes here
                }
            }

        // Other code to add the hotel to db goes here
    }

在调试器中我看到 Request.Files.Count 总是为零,我不知道为什么? 我已经从另一个 MVC4 项目中复制了视图和控制器代码,它工作正常。

Key Value
Request POST /HotelManager/Create HTTP/1.1
Accept  text/html, application/xhtml+xml, */*
Referer http://localhost:4976/HotelManager/Create
Accept-Language en-US,en;q=0.5
User-Agent  Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; ASU2JS; rv:11.0) like Gecko
Content-Type    multipart/form-data; boundary=---------------------------7de207070a24
Accept-Encoding gzip, deflate
Host    localhost:4976
Content-Length  946
DNT 1

在 IE Developer 窗口中,我看到表单主体是空的。

【问题讨论】:

    标签: asp.net-mvc http-headers asp.net-mvc-5 asp.net-mvc-5.1 enctype


    【解决方案1】:

    我浪费了这么多时间,结果我需要做的只是使用 name 属性

     <input type="file" name="somename"> 
    

    我的新项目中缺少 name 属性。

    【讨论】:

    • 呃!在这上面浪费了至少 30 分钟。好收获!
    • 发生这种情况的另一种可能性是,如果您错过了表单标签上的 enctype="multipart/form-data"。
    【解决方案2】:

    你需要改变post方式

    需要补充:

    new { enctype = "multipart/form-data" }
    

    这是完整的表单标签

    @using (Html.BeginForm("Cars", "Expense", FormMethod.Post, 
          new { enctype = "multipart/form-data" }))
    

    See how can you post multiple files

    【讨论】:

    • 这可能不是 OP 的问题,但它解决了我的问题...不知道 enctype 是必要的。
    • 这里也一样。由于我很少使用它,我忘了添加 enctype
    • ==> enctype="multipart/form-data" 在表单标签上
    【解决方案3】:

    添加 您需要更改 post 方法。

    需要补充:

    new { enctype = "multipart/form-data" }
    

    这是完整的表单标签

    @using (Html.BeginForm("Cars", "Expense", FormMethod.Post, 
          new { enctype = "multipart/form-data" }))
    

    这些行是必不可少的;没有它们,您将无法保存文件类型的输入。我坐了 6 个小时试图弄清楚这一点。

    【讨论】:

      【解决方案4】:

      Request.Files.Count 变为 0 的原因之一是当您尝试上传大于 Web.config 文件中指定的 maxRequestLength 的文件时。如果您遇到 Request.Files.Count 问题,请务必检查此值。

      <configuration>
        <system.web>
          <httpRuntime maxRequestLength="4096" />
        </system.web>
      </configuration>
      

      更新: 此答案可能与 OP 的问题没有直接关系,但由于谷歌将此帖子列为 Request.Files.Count=0 问题的顶部,因此发布了对我有用的答案。

      【讨论】:

      • 这个答案可能与 OP 的问题没有直接关系,但由于谷歌将此帖子列为 Request.Files.Count=0 问题的顶部,因此发布了对我有用的答案。
      【解决方案5】:

      在上传图片时,我发现了一种情况。 如果您将图像存储在 DBContext 中并使用 async-await 调用任何 API,则在调用任何 API 之前将图像保存在数据库中。否则 Content-length 变为 0。我已经清楚地调试了这一点,然后实现了。我们需要在异步调用之前对数据库执行保存操作。 但是,文件输入类型和 Post 方法必须具有 name 属性,并且 HTML beginform 中必须需要 enctype multipart/form-data。

      您也可以尝试通过以下方式获取文件库,

      HttpPostedFileBase fileBase = Request.Files["file_name"];
      

      【讨论】:

        猜你喜欢
        • 2021-08-01
        • 2013-04-12
        • 2015-08-20
        • 2015-10-19
        • 1970-01-01
        • 1970-01-01
        • 2016-08-25
        • 1970-01-01
        • 2017-02-15
        相关资源
        最近更新 更多