Request.Files.Count始终为0,而在MVC 5上传图片 [英] Request.Files.Count always 0 while uploading image in MVC 5
问题描述
我有一些字符串字段和图像模型的帖子控制器。确切的相同code适用于MVC4但在MVC 5 Request.Files.Count始终为0
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
我的模型具有字节[]的图像,而不是HttpPostedFileBase
My model has byte[] for image rather than 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 =假,但没有用。
I have tried omitting data_ajax = "false" but no use.
我的帖子控制器如下:
[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项目中,它工作正常,两个视图和控制器code。
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.
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开发窗口,我看到表单主体是空的。
And in the IE Developer window I see that the form body is empty.
推荐答案
我浪费了那么多时间和事实证明,所有我需要做的是使用的名称属性以及
I wasted so much time and it turns out that all I needed to do was to use the name attribute as well
<input type="file" name="somename">
的名称属性缺失在我的新项目。
the name attribute was missing in my new project.
这篇关于Request.Files.Count始终为0,而在MVC 5上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!