码迷,mamicode.com
首页 > 其他好文 > 详细

Razor强类型视图下的文件上传

时间:2014-09-15 12:42:18      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:http   io   os   ar   for   文件   art   sp   cti   

域模型Users.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FileUpload.Models
{
    public class Users
    {
        public string UserName { get; set; }

        public string Password { get; set; }
    }
}

 

控制器:FilesUploadController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FileUpload.Models;
using System.IO;

namespace FileUpload.Areas.FileUploads.Controllers
{
    public class FilesUploadController : Controller
    {
        //
        // GET: /FileUploads/FilesUpload/

        #region 单个文件上传
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 单个文件上传的方法
        [HttpPost]
        public ActionResult FileUpload(Users user, HttpPostedFileBase uploadFile)
        {
            if (uploadFile != null)
            {
                string username = user.UserName;
                string password = user.Password;
                if (uploadFile.ContentLength > 0)
                {
                    //获得保存路径
                    string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                   Path.GetFileName(uploadFile.FileName));
                    uploadFile.SaveAs(filePath);
                }

            }
            return View();
        }
        #endregion

        #region 多个文件上传
        public ActionResult MultiFiles()
        {
            return View();
        }
        #endregion

        #region 上传多个文件方法
        public ActionResult MultiUpload(Users user, IEnumerable<HttpPostedFileBase> files)
        {
            string pathForSaving = Server.MapPath("~/Uploads");

            string username = user.UserName;
            string password = user.Password;

            if (this.CreateFolderIfNeeded(pathForSaving))
            {
                foreach (var file in files)
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        var path = Path.Combine(pathForSaving, file.FileName);
                        file.SaveAs(path);
                    }
                }
            }

            return RedirectToAction("Index");
        }
        #endregion

        #region 检查是否要创建上传文件夹
        private bool CreateFolderIfNeeded(string path)
        {
            bool result = true;
            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception)
                {
                    //TODO:处理异常
                    result = false;
                }
            }
            return result;
        }
        #endregion

    }
}

前端页面:Index.cshtml

@model FileUpload.Models.Users
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("MultiUpload", "FilesUpload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBoxFor(r=>r.UserName)
    @Html.PasswordFor(r=>r.Password)
    
    <input type="file" name="files" id="file1" /><br />
    <input type="file" name="files" id="file2" /><br />
    <input type="file" name="files" id="file3" /><br />
    <input type="submit" value="同时上传多个文件" />
}

Razor强类型视图下的文件上传

标签:http   io   os   ar   for   文件   art   sp   cti   

原文地址:http://www.cnblogs.com/leon719/p/3972537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!