asp.net-mvc Request.Files.Count 在 MVC 5 中上传图像时始终为 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22435971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Request.Files.Count always 0 while uploading image in MVC 5
提问by indichimp
I have a Post controller on a model with some string fields and an image. Exact identical code works on MVC4 but in MVC 5 the Request.Files.Count is always 0
我在一个带有一些字符串字段和图像的模型上有一个 Post 控制器。完全相同的代码适用于 MVC4,但在 MVC 5 中 Request.Files.Count 始终为 0
My model has byte[] for image rather than HttpPostedFileBase
我的模型有用于图像的字节 [] 而不是 HttpPostedFileBase
My View:
我的看法:
@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" />
}
I have tried omitting data_ajax = "false" but no use.
我试过省略 data_ajax = "false" 但没有用。
My Post Controller is as follows:
我的帖子控制器如下:
[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
}
In the debugger I see that the Request.Files.Count is always zero, I don't know why? I have copied over both the view and controller code from another MVC4 project where it works fine.
在调试器中我看到 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
And in the IE Developer window I see that the form body is empty.
在 IE Developer 窗口中,我看到表单主体是空的。
回答by indichimp
I wasted so much time and it turns out that all I needed to do was to use the name attribute as well
我浪费了太多时间,结果我需要做的只是使用 name 属性
<input type="file" name="somename">
the name attribute was missing in my new project.
我的新项目中缺少 name 属性。
回答by Ali Adravi
You need to change the post method
你需要改变post方法
Need to add:
需要补充:
new { enctype = "multipart/form-data" }
Here is complete form tag
这是完整的表单标签
@using (Html.BeginForm("Cars", "Expense", FormMethod.Post,
new { enctype = "multipart/form-data" }))
回答by Sanvir
To add on You need to change the post method.
要添加您需要更改 post 方法。
Need to add:
需要补充:
new { enctype = "multipart/form-data" }
Here is complete form tag
这是完整的表单标签
@using (Html.BeginForm("Cars", "Expense", FormMethod.Post,
new { enctype = "multipart/form-data" }))
Those lines are essential; without them, you will not be able to save an input of type file. I sat for 6 hours trying to figure that out.
这些线条是必不可少的;没有它们,您将无法保存文件类型的输入。我坐了 6 个小时试图弄清楚这一点。
回答by Gayan Dasanayake
One of the reasons why Request.Files.Count becomes 0 is when you try to upload a file larger than the maxRequestLength specified on Web.config file. Make sure to check this value if you are having an issue with Request.Files.Count.
Request.Files.Count 变为 0 的原因之一是当您尝试上传大于 Web.config 文件中指定的 maxRequestLength 的文件时。如果 Request.Files.Count 有问题,请务必检查此值。
<configuration>
<system.web>
<httpRuntime maxRequestLength="4096" />
</system.web>
</configuration>
UPDATE: This answer might not be directly related to OP's issue, but since google lists this post top for Request.Files.Count=0 issue, posted the answer which worked for me.
更新:这个答案可能与 OP 的问题没有直接关系,但由于谷歌将这个帖子列为 Request.Files.Count=0 问题的顶部,发布了对我有用的答案。

