ASP.NET MVC 上传文件

这几天练习的ASP.NET MVC项目中碰到了文件上传的问题,分享一下:

View:


1<form action="/AdminImage/Index/" method="post" enctype="multipart/form-data">
2    <div>
3        Please select a file to upload:&nbsp;&nbsp;
4        <input type="file" id="fileImage" name="fileImage" />&nbsp;&nbsp;
5        <input type="submit" value="Upload" />
6    </div>
7</form>

 

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        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值