域模型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强类型视图下的文件上传的更多相关文章

  1. centos 6.5下安装文件上传下载服务

    centos 6.5下安装文件上传下载服务 由于每次在CentOS中要下载一些配置文件到物理机,和上传一些文件到服务器,导致来回的开启ftp软件有点麻烦,这里我们可以使用文件上传下载服务,来解决上传和 ...

  2. Struts2框架下的文件上传文件类型、名称约定

    Struts2框架下的文件上传机制:1.通过multipart/form-data form提交文件到服务器2.文件名是通过什么地方设置的?在strust2的FileUploadInterceptor ...

  3. linux下将文件上传到svn服务器

    linux下将文件上传到svn服务器 摘自:https://blog.csdn.net/sky_yangge/article/details/41544773 2014年11月27日 16:47:57 ...

  4. ASP.NET MVC下使用文件上传

    这里我通过使用uploadify组件来实现异步无刷新多文件上传功能. 1.首先下载组件包uploadify,我这里使用的版本是3.1 2.下载后解压,将组件包拷贝到MVC项目中 3.  根目录下添加新 ...

  5. 第一零四天上课 PHP TP框架下的文件上传

    控制器代码(TestController.class.php) <?php namespace Home\Controller; use Home\Controller\EmptyControl ...

  6. ssm框架下实现文件上传

      1.由于ssm框架是使用Maven进行管理的,文件上传所需要的jar包利用pom.xml进行添加,如下所示: <properties> <commons-fileupload.v ...

  7. ssm框架下的文件上传和文件下载

    最近在做一个ssm的项目,遇到了添加附件和下载的功能,在网上查了很多资料,发现很多都不好用,经过摸索,发现了一套简便的方法,和大家分享一下. 1.在自己已经构建好的maven  web项目中 pom. ...

  8. 微信小程序环境下将文件上传到 OSS

    步骤 1: 配置 Bucket 跨域 客户端进行表单直传到 OSS 时,会从浏览器向 OSS 发送带有 Origin 的请求消息.OSS 对带有 Origin 头的请求消息会进行跨域规则(CORS)的 ...

  9. 将windows下的文件上传到Linux服务器上

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lx_Frolf/article/deta ...

随机推荐

  1. C++中的栈和队列

    使用标准库的栈和队列时,先包含相关的头文件 #include<stack> #include<queue> 定义栈如下: stack<int> stk; 定义队列如 ...

  2. shell中常用的特殊字符

    (1) * 代表0到无穷个任意字符 (2)?代表任意一个字符 (3)代表括号内任意一个字符 (4)[ - ] 代表一个范围中的任意一个字符 如[0-9] 即是代表0-9之间的一个数 (5)[^] 反向 ...

  3. angularJS使用$watch监控数据模型的变化

    使用$watch监控数据模型的变化 在scope 内置的全部函数中,用得最多的可能就是$watch 函数了.当你的数据模型中某一部分发生变化时,$watch 函数能够向你发出通知.你能够监控单个对象的 ...

  4. linux之vim配置

    代码自动补全和代码跳转阅读,应该是作为程序员最常用的功能之一了,具体二者是指什么我就不解释了.微软的Visual Studio就是靠这两样必杀技牢牢占据着广大windows程序员的心(这里面要有强大的 ...

  5. 使用命令行将Excel数据表导入Mysql中的方法小结

    从Excel数据表导入MySQL,已经做过好几次了,但每次都会碰到各种问题:invalid utf8 character string, data too long, ...,浪费了不少时间 为了提高 ...

  6. SQL Server 动态行转列(轉載)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段; 方法二:使用拼接SQL, ...

  7. Problem A: 走迷宫问题

    Problem A: 走迷宫问题Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 9 Solved: 3[Submit][Status][Web Board] ...

  8. [NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)

    一开始觉得是网络流..仔细一看应该是最短路,再看数据范围..呵呵不会写...这道题是最大生成树+最近公共祖先.第一次写..表示各种乱.. 因为要求运输货物质量最大,所以路径一定是在最大生成树上的.然后 ...

  9. Lucence.net索引技术 二

    一. Lucene索引创建和优化 [版本2.9.0以上] Lucene索引的创建首先需要取得几个必须的对象: 1.分词器//可以采用其他的中文分词器 StandardAnalyzer analyzer ...

  10. PHP输出中文乱码的解决方法

    最近在windows上发现PHP程序中输出来的中文有乱码的情况. 看了很多帖子资料说可以在页面上添加: http://www.cnblogs.com/leandro/archive/2008/04/2 ...