亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

mvc3上傳文件問題

mvc3上傳文件問題

呼啦一陣風 2018-12-07 03:57:44
@using (Html.BeginForm(new { enctype = "multipart/form-data" })) { @Html.ValidationSummary(true) <fieldset> <legend>Question</legend> <div class="editor-label"> @Html.LabelFor(model => model.Title) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.Title, new { style = "width: 400px" }) @Html.ValidationMessageFor(model => model.Title) </div> <div class="editor-label"> 添加問題圖片: </div> <div class="editor-field"> 選擇上傳文件:<input name="file" type="file" id="file" /> </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } controller: // POST: /Question/Create Question question, [HttpPost] public ActionResult Create(Question question, HttpPostedFileBase file) { try { if (ModelState.IsValid) { if (file !=null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Resources"), fileName); file.SaveAs(path); QuestionResource resource = new QuestionResource(); resource.Url = path; question.Resources.Add(resource); } unitOfWork.QuestionRepository.Insert(question); unitOfWork.Save(); return RedirectToAction("Index"); } } catch(Exception ex) { //Log the error (add a variable name after DataException) ModelState.AddModelError("", question.Title + " " + question.Description + " " + file.ContentLength + " " + ex.Message + " Unable to save changes."); } return View(question); } 在控制器中:HttpPostedFileBase file獲取不到上傳的文件。調試顯示null。 ? 如何獲取到上傳的文件?謝謝
查看完整描述

11 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

你 這樣 讀取this.HttpContext.Request.Files。不要加到參數里。

查看完整回答
反對 回復 2019-01-21
?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

打不開。一閃就消失了

查看完整回答
反對 回復 2019-01-21
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

@二手 程序員: 那圖片上傳成功了嗎

查看完整回答
反對 回復 2019-01-21
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

@二手 程序員: 都打不開??博客園其他頁面能打開嗎?

查看完整回答
反對 回復 2019-01-21
?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

@二手 程序員: 我這邊打開很快啊,你再試試吧

查看完整回答
反對 回復 2019-01-21
  • 11 回答
  • 0 關注
  • 403 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號