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

时间:2022-03-22 14:48:26

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

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

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

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

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)页面的源码

单文件上传的页面

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

多文件上传的页面

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

成功页面

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

5)文件上传的效果图

a、单文件上传效果图

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

b、多文件上传效果图

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

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

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

相关文章