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

时间:2023-03-08 23:15:15
Razor强类型视图下的文件上传

域模型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="同时上传多个文件" />
}