域模型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. AN ESAY HIT COUNTER

    <?php $counts = ("hitcounter.txt"); $hits = file($counts); $hits[0] ++; $fp = fopen($co ...

  2. poj 3501 Escape from Enemy Territory 预处理+二分+bfs

    传送门 给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标. 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离. 首先预处理出来地图上的所有点到敌人的最短距离, ...

  3. Use API to retrieve data from internet

    Reference: Working with APIs Many big companies and organizations provide API for us to retrieve dat ...

  4. SQL Server Mysql primary key可更新性分析

    SQL Server: 一般来说SQL Server 中表的主键是支持更新操作的.但是如果这个主键是由identity(1,1)这类的方式生成的话它是不可更新的. Mysql : Mysql 中表的主 ...

  5. Window下 Qt 编译MySQL驱动(居然用到了动态库格式转换工具)

    一步步在Window下开发Qt 今天开始安装MySQL,看了些关于MySQL安装的博文,方法大致相同,但是遇到的细节问题各有不同,或者没有讲全面,下面来说说个人的安装过程及遇到的问题. 1.首先下载, ...

  6. XmlDocument加载有Xmlns的xml文档,使用Xpath

    using System; using System.IO; using System.Xml; public class Sample { public static void Main() { X ...

  7. float 浮点数与零值0比较大小

    float x: 千万不要写x==0; 写出float x 与“零值”比较的if语句——一道面试题分析 写出float  x 与“零值”比较的if语句 请写出 float  x 与“零值”比较的 if ...

  8. elk 分布式部署

    这个logstash 读取日志 是增量的 还是怎么读的? 定时每秒读增量 机器配置: elasticsearch-192.168.32.80 elasticsearch-192.168.32.81 e ...

  9. j2se总结

    j2se总结 经过一周多的时间完成了j2se基础的学习,个人感觉最大的收获不是对j2se的语法了解了多少,而是对面向对象的认识再一次加深了.面向对象,让类和对象成为一切. 下边是对j2se的一部分的总 ...

  10. Linux系统源码安装过程中的prefix选项

    在linux和unix环境中,源码安装是最常用的软件安装方式,一些软件除了提供源码外,也提供各种发行版的二进制安装包(如基于redhat包管理工具的rpm包),但强烈建议使用源码安装方式.原因是:(1 ...