您的位置:首页 > 编程语言 > ASP

ASP.NET MVC 上传文件

2009-06-18 05:50 309 查看
这几天练习的ASP.NET MVC项目中碰到了文件上传的问题,分享一下:

View:




View
1

<form action="/AdminImage/Index/" method="post" enctype="multipart/form-data">
2

<div>
3

Please select a file to upload:  
4

<input type="file" id="fileImage" name="fileImage" />  
5

<input type="submit" value="Upload" />
6

</div>
7

</form>

Controller:




Controller
1

public ActionResult Index()
2





{
3

return View();
4

}
5

[AcceptVerbs(HttpVerbs.Post)]
6

public ActionResult Index(FormCollection collection)
7





{
8

if (Request.Files.Count == 0)
9





{
10

return View();
11

}
12

var c = Request.Files[0];
13

if (c != null && c.ContentLength > 0)
14





{
15

int lastSlashIndex = c.FileName.LastIndexOf("//");
16

string fileName = c.FileName.Substring(lastSlashIndex + 1, c.FileName.Length - lastSlashIndex - 1);
17

fileName = Path.Combine(CommonUtility.DocImagePath, fileName);
18

c.SaveAs(fileName);
19

}
20

return View();
21

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: