gpt4 book ai didi

asp.net-mvc - Request.Files.Count 在 MVC 5 中上传图像时始终为 0

转载 作者:行者123 更新时间:2023-12-03 10:44:13 29 4
gpt4 key购买 nike

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

我的模型有用于图像的字节 [] 而不是 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"但没有用。

我的帖子 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 项目中复制了 View 和 Controller 代码,它可以正常工作。
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 窗口中,我看到表单主体是空的。

最佳答案

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

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

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

关于asp.net-mvc - Request.Files.Count 在 MVC 5 中上传图像时始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22435971/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com