mvc多个文件上传和单个文件上传
2016-02-10 20:03
183 查看
mvc多个文件上传和单个文件上传
控制器中
控制器中
public ActionResult FileUP(HttpPostedFileBase file) { var files = Request.Files; if (files != null && files.Count > 0) { #region 执行多个文件上传 for (int i = 0; i < files.Count; i++) { HttpPostedFileBase fileitem = files[i]; //判定文件的大小 string strExtension = Path.GetExtension(fileitem.FileName); double dFileSize = fileitem.ContentLength; if (dFileSize > 5242880)//1024*1024*5) { return Content("<script>alert('" + fileitem.FileName + "文件大于5MB')</script>"); } else { //创建文件 string filePath = "~/images/Student/"; Directory.CreateDirectory(Server.MapPath(filePath)); //创建唯一的文件名 string fileName = Guid.NewGuid().ToString(); string fFullDir = filePath + fileName + strExtension; fileitem.SaveAs(Server.MapPath(fFullDir)); } } #endregion } else { #region 执行单个文件上传 if (file != null) { //可以判断它的大小格式 //创建文件夹 string filePath = "~/images/Student/"; Directory.CreateDirectory(Server.MapPath(filePath)); //string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string fileName = Guid.NewGuid().ToString(); file.SaveAs(Server.MapPath(filePath + fileName + ".png")); return Content("<script>alert('上传成功!');location.href=" + Url.Content(filePath) + "</script>"); } #endregion } return View(); }
相关文章推荐
- X - A == B ?(第二季水)
- poj 1163 The Triangle
- KVC(键值编码)详解
- CodeForces 119A Epic Game
- 书
- [笔记][Web]利用JS生成博文目录及CSS定制博客
- Java基础 数列排序
- ViewDragHelper 使用
- Dalvik和JVM的区别
- Chapter 1-02
- Chapter 1-01
- 利用DelayQueue实现延时消息队列(简易版MQ)
- PID控制器的数字实现及C语法讲解
- 类的编写
- const
- 蓝桥基础练习学习记录
- linux下安装php7+mysql5.7+nginx
- handler用法
- Rust学习笔记
- Atitit.nosql api 标准化 以及nosql数据库的实现模型分类差异