Asp.net-Mvc
在 MVC 5 中上傳圖像時,Request.Files.Count 始終為 0
我在帶有一些字元串欄位和圖像的模型上有一個 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” 但沒有用。
我的文章控制器如下:
[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 視窗中,我看到表單主體是空的。
我浪費了很多時間,結果我需要做的只是使用 name 屬性
<input type="file" name="somename">我的新項目中缺少 name 屬性。