首页 新闻 会员 周边

mvc3上传文件问题

0
悬赏园豆:5 [已解决问题] 解决于 2012-04-16 14:35
@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。

 

如何获取到上传的文件?谢谢

二手 程序员的主页 二手 程序员 | 初学一级 | 园豆:159
提问于:2012-04-12 13:01
< >
分享
最佳答案
0

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

收获园豆:5
無限遐想 | 老鸟四级 |园豆:3740 | 2012-04-12 13:14

还是取不到值

二手 程序员 | 园豆:159 (初学一级) | 2012-04-12 14:36

@二手 程序员: 加一個post。是post方法嗎?

@using (Html.BeginForm("Create", "Feedback", FormMethod.Post, new { enctype = "multipart/form-data" }))

我這裡這麼用沒有什麽問題。

無限遐想 | 园豆:3740 (老鸟四级) | 2012-04-12 15:03

@無限遐想: 

<input name="file" type="file" id="file" /> 这个也是这样写的?
二手 程序员 | 园豆:159 (初学一级) | 2012-04-12 15:12

@二手 程序员: 

<input type="file" name="file"' + number + '" ' id=‘“+ number + '" />對哦。基本上市這樣。

         public ActionResult Create(Feedback freedback)
         {

this.HttpContext.Request.Files

}

無限遐想 | 园豆:3740 (老鸟四级) | 2012-04-12 15:18

@無限遐想: 测了一下,一定要写上FormMethod.Post就可以了。 不是默认就是post方式吗

二手 程序员 | 园豆:159 (初学一级) | 2012-04-12 17:05
其他回答(1)
0
artwl | 园豆:16736 (专家六级) | 2012-04-12 13:40

打不开。一闪就消失了

支持(0) 反对(0) 二手 程序员 | 园豆:159 (初学一级) | 2012-04-12 14:36

@二手 程序员: 那图片上传成功了吗

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2012-04-12 14:37

@artwl: 没有成功。我是说你这个连接一打开就变成空白页了。

支持(0) 反对(0) 二手 程序员 | 园豆:159 (初学一级) | 2012-04-12 14:49

@artwl: 是。这个地址,几个浏览器都打开开

支持(0) 反对(0) 二手 程序员 | 园豆:159 (初学一级) | 2012-04-12 15:02

@artwl: 打不开

支持(0) 反对(0) 二手 程序员 | 园豆:159 (初学一级) | 2012-04-12 15:03

@二手 程序员: 都打不开??博客园其他页面能打开吗?

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2012-04-12 15:21

@artwl: 可以

支持(0) 反对(0) 二手 程序员 | 园豆:159 (初学一级) | 2012-04-12 15:27

@二手 程序员: 我这边打开很快啊,你再试试吧

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2012-04-12 15:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册