控制器

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.IO;
   4:  using System.Linq;
   5:  using System.Web;
   6:  using System.Web.Mvc;
   7:   
   8:  namespace MvcApplication2.Controllers
   9:  {
  10:      public class HomeController : Controller
  11:      {
  12:          public ActionResult Index()
  13:          {
  14:              return View();
  15:          }
  16:   
  17:          public ActionResult FileUploads()
  18:          {
  19:              string pathForSaving = Server.MapPath("~/Uploads");
  20:              if (this.CreateFolderIfNeeded(pathForSaving))
  21:              {
  22:                  foreach (string file in Request.Files)
  23:                  {
  24:                      HttpPostedFileBase uploadFile = Request.Files[file] as HttpPostedFileBase;
  25:                      if (uploadFile != null && uploadFile.ContentLength > 0)
  26:                      {
  27:                          var path = Path.Combine(pathForSaving, uploadFile.FileName);
  28:                          uploadFile.SaveAs(path);
  29:                      }
  30:                  }
  31:              }
  32:              return RedirectToAction("Index");
  33:          }
  34:   
  35:          // 检查是否要创建上传文件夹
  36:          private bool CreateFolderIfNeeded(string path)
  37:          {
  38:              bool result = true;
  39:              if (!Directory.Exists(path))
  40:              {
  41:                  try
  42:                  {
  43:                      Directory.CreateDirectory(path);
  44:                  }
  45:                  catch (Exception)
  46:                  {
  47:                      //TODO:处理异常
  48:                      result = false;
  49:                  }
  50:              }
  51:              return result;
  52:          }
  53:      }
  54:  } 

□ Home/Index.cshtml视图

   1:  @{
   2:      ViewBag.Title = "Index";
   3:      Layout = "~/Views/Shared/_Layout.cshtml";
   4:  }
   5:   
   6:  @using (Html.BeginForm("FileUploads", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
   7:  {
   8:      <input type="file" name="files1" id="file1" /><br/>
   9:      <input type="file" name="files2" id="file2" /><br/>
  10:      <input type="file" name="files3" id="file3" /><br/>
  11:      <input type="submit" value="同时上传多个文件" />
  12:  }
注意: 
name属性值可以不同

转载自:http://www.csharpwin.com/dotnetspace/13612r1616.shtml

MVC文件上传 - 使用Request.Files上传多个文件的更多相关文章

  1. MVC文件上传03-使用Request.Files上传多个文件

    本篇体验在控制器方法中使用controllerContext.HttpContext.Request.Files上传多个文件.兄弟篇为: MVC文件上传01-使用jquery异步上传并客户端验证类型和 ...

  2. Django:上传文件或者图片时request.FILES的值为空

    在form表单中加上属性 enctype="multipart/form-data"

  3. html5表单上传控件Files筛选指定格式的文件:accept属性过滤excel文件

    摘自:http://blog.csdn.net/jyy_12/article/details/9851349 (IE9及以下不支持下面这些功能,其它浏览器最新版本均已支持.) 1.允许上传文件数量 允 ...

  4. asp.net 页面上传文件控件后台代码Request.Files获取不到

    今天开发中遇到页面文件上传控件选择了文件,而后台Request.Files.Count取值为0,之前开发中遇到过几次,老是忘掉,今天记下来. html: <input type="fi ...

  5. flask上传文件时request.files为空的解决办法

    在做上传文件的时候遇到request.files是空 原因在于html中的表单form没有指明 enctype="multipart/form-data" <form met ...

  6. 文件的上传(表单上传和ajax文件异步上传)

    项目中用户上传总是少不了的,下面就主要的列举一下表单上传和ajax上传!注意: context.Request.Files不适合对大文件进行操作,下面列举的主要对于小文件上传的处理! 资源下载: 一. ...

  7. Asp.net上传文件Request.files获取不到文件

    使用ftp上传文件,并且Request.files获取文件,今天发现获取到的文件个数始终是0个,查了下原来form标签中需加入enctype=”multipart/form-data”,呵呵了 < ...

  8. 多选文件批量上传前端(ajax*formdata)+后台(Request.Files[i])---input+ajax原生上传

    1.配置Web.config;设定上传文件大小 <system.web> <!--上传1000M限制(https://www.cnblogs.com/Joans/p/4315411. ...

  9. MVC文件上传 - 使用jquery异步上传并客户端验证类型和大小

    本篇体验MVC上传文件,从表单上传过渡到jquery异步上传. MVC最基本的上传文件是通过form表单提交方式 □ 前台视图部分 <% using(Html.BeginForm("F ...

随机推荐

  1. Oracle 错误码

    Oracle作为一款比较优秀同时也比较难以掌握的大型数据库,在我们学习使用的过程中,不可避免的会遇到一些错误,为此 Oracle 给出了一套完备的错误消息提示机制 我们可以根据Oracle给出的消息提 ...

  2. code:blocks 编译环境设置

    1.  支持C99 在菜单settings->compiler settings->comiler settings->Other options 添加: -std=c99 2. 支 ...

  3. php微信简单接口

    <?php $appid = "xxxxxxxxxx"; $secret = "xxxxxxxxxxxxxxxxxxxxxxxxx"; $url = &q ...

  4. await使用中的阻塞和并发

    本文讨论,通过将Lambda还原成最普通的代码段,来解释上篇提出的疑问.并更正上篇中一些不太正确的写法.最后会给出无需等待Async方法返回值时,对Async方法使用await的建议,供大家参考.第一 ...

  5. WCF入门教程[WCF基本应用]

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

  6. oracle 更改SQL提示

    在oracle里面修改SQL提示为数据库名称: SQL>set SQLPROMPT "TEST>"

  7. 在线安装maven插件问题:Cannot complete the install because one or more required items could not be found.

    用Eclipse在线安装的方式:Help-->Install  New Software 地址输入:http://m2eclipse.sonatype.org/sites/m2e/,列表中打勾勾 ...

  8. RAW碰到的问题与功能的测试

    碰到的问题太多: 1. 没人告诉我,应该在CreateFile之前,先InstallDriver2. 签名死活不行(好像是反斜杠问题)3. 注册码错误4. 怀疑ANSI与Unicode注册码的问题5. ...

  9. 借鉴DP思想: HouseRobberIII

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  10. php 读取 word

    ---恢复内容开始--- 首先安装com扩展: php.ini php.ini 确保有此语句 [PHP_COM_DOTNET] extension=php_com_dotnet.dll   php.i ...