asp.net.mvc 的单文件上传和多文件上传的简单例子

首先打开vs2012,创建空的mvc4项目,名称为MVCStudy,选择基本模板

1)创建项目后,基本结构是这样的

2)建立对应的HomeController,视图index、fileupload、success、error页面

3)控制器源码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Services.Protocols;


namespace MvcStudy.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 成功返回页面
        /// </summary>
        /// <returns></returns>
        public ActionResult success() {
            return View();
        }

        /// <summary>
        /// 失败返回页面
        /// </summary>
        /// <returns></returns>
        public ActionResult error() {
            return View();
        }

        /// <summary>
        /// 单文件上传
        /// </summary>
        /// <param name="upfile">上传文件的对象</param>
        /// <returns></returns>
        public ActionResult FileUpload(HttpPostedFileBase upfile)
        {
            try
            {
                //文件不为空
                if (upfile != null)
                {
                    //创建年月文件夹,如201605
                    string fileFolder = DateTime.Now.ToString("yyyyMM");
                    //拼接保存时根路径,比如:D:Work/MVCTest/Uploads/201605
                    string pathForSaving = Server.MapPath("~/Uploads/") + fileFolder;
                    //判断文件夹是否存在,否则创建文件夹
                    if (this.CreateFolderIsNeeded(pathForSaving))
                    {
                        //判断上传文件的大小
                        if (upfile.ContentLength > 0)
                        {
                            //重新组合成一个存放路径,根路径+文件名称
                            string filepath = Path.Combine(pathForSaving, upfile.FileName);
                            upfile.SaveAs(filepath);
                        }
                    }
                    //返回成功提示页面
                    return RedirectToAction("success");
                }
            }
            catch (Exception e)
            {
                return RedirectToAction("error");
            }
            return View();
        }

        /// <summary>
        /// 多文件上传的方法
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public ActionResult MultiUpload(IEnumerable<HttpPostedFileBase> files)
        {
            try {
                //判断多个文件是否为空
                if (files != null) {
                    //组合成文件的存放路径
                    string fileFolder = DateTime.Now.ToString("yyyyMM");
                    string pathForSaving = Server.MapPath("~/Uploads/") + fileFolder;
                    //判断存放路径是否存在,否则创建对应路径
                    if (this.CreateFolderIsNeeded(pathForSaving))
                    {
                        //循环遍历文件,并保存
                        foreach (var file in files)
                        {
                            if (file != null && file.ContentLength > 0)
                            {
                                var path = Path.Combine(pathForSaving, file.FileName);
                                file.SaveAs(path);
                            }
                        }
                    }
                    return RedirectToAction("success");
                }
                 
            }
            catch(Exception e){
                return RedirectToAction("error");
            }
            return View();
        }

        /// <summary>
        /// 判断是否需要创建文件夹
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public bool CreateFolderIsNeeded(string path) {
            bool result = true;
            if (!Directory.Exists(path)) {
                try {
                    Directory.CreateDirectory(path);
                }
                catch(Exception ex){
                    result = false;
                }
            }
            return result;
        }
    }
}
4)页面的源码

单文件上传的页面

多文件上传的页面

成功页面

 

5)文件上传的效果图

a、单文件上传效果图

b、多文件上传效果图

c、上传后在项目路径下文件的效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值