asp.net mvc 多文件上传
2015-09-18 15:59
393 查看
@{ ViewBag.Title = "多文件上传测试"; } <h2>多文件上传测试</h2> <form action="/Demo/Index" method="post" enctype="multipart/form-data"> <input type="file" /><br /> <input type="file" /><br /> <input type="file" /><br /> <input type="submit" value="上传" /> </form>
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using ZhaoWoAPI.Models; namespace ZhaoWoAPI.Controllers { public class DemoController : Controller { [HttpPost] public ActionResult Index(string content) { try { FileLogModel.Log("========================开始上传图片=====================:content=" + content); HttpFileCollectionBase files = Request.Files; FileLogModel.Log("==================上传图片数量为:" + files.Count+"==========================="); if(files.Count<=0) { FileLogModel.Log("=================无上传图片===================================" ); return Content("0"); } for (int i = 0; i < files.Count; i++) { HttpPostedFileBase hpf = files[i]; if (hpf.ContentLength <= 0) { continue; } var extension = Path.GetExtension(hpf.FileName); FileLogModel.Log("=====================文件后缀名:"+extension+"=========================="); string datename = DateTime.Now.ToString("yyyy-MM-dd"); string path = Server.MapPath("/Resource/SendInfo/" + datename + "/"); FileLogModel.Log("=====================上传路径:" + path + "=========================="); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string newfilename = path + ZhaoWoCommon.GUIDTools.GetGUID() + extension; FileLogModel.Log("=====================完整路径:" + newfilename + "=========================="); hpf.SaveAs(newfilename); } FileLogModel.Log("=====================上传成功==========================" ); return Content("ok"); } catch (Exception ex) { FileLogModel.Log("上传失败"+ex.Message ); return Content("error"); } } public ActionResult Test() { return View(); } } }
相关文章推荐
- ASP.NET MVC5网站开发咨询管理的架构(十一)
- ASP.NET MVC5网站开发修改及删除文章(十)
- ASP.NET新建解决方案和网站
- ASP.net Web API允许跨域访问解决办法
- ASP.NET动态网站制作(2)--css(1)
- asp.net之ajax
- asp.net web实现定时器功能
- asp.net MVC4在Action间跳转 RedirectToAction 传值参数问题
- CasperJS needs PhantomJS v1.× 错误解决方法
- 在Windows上安装casperjs
- ASP.NET窗体身份验证详解
- ASP替换、保存远程图片实现代码
- 11种ASP连接数据库的方法
- 怎么设置远程ASP网页和MYSQL数据库的连接
- ASP.NET Web API实现微信公众平台开发(一)服务器验证
- ASP.NET如何使用JSON
- ASP.NET如何使用JSON
- ASP 编码转换(乱码问题解决)
- asp网页居中代码
- ASP.NET Web API身份验证和授权