MVC文件上传03-使用Request.Files上传多个文件
本篇体验在控制器方法中使用controllerContext.HttpContext.Request.Files上传多个文件。兄弟篇为:
MVC文件上传01-使用jquery异步上传并客户端验证类型和大小
MVC文件上传02-使用HttpPostedFileBase上传多个文件
□ 控制器
- 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: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ 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: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
注意:
name属性值可以不同
参考资料
※ ASP.NET MVC - 不使用HttpPostedFileBase 处理文件上传
MVC文件上传03-使用Request.Files上传多个文件的更多相关文章
- MVC文件上传 - 使用Request.Files上传多个文件
控制器 1: using System; 2: using System.Collections.Generic; 3: using System.IO; 4: using System.Linq; ...
- asp.net 页面上传文件控件后台代码Request.Files获取不到
今天开发中遇到页面文件上传控件选择了文件,而后台Request.Files.Count取值为0,之前开发中遇到过几次,老是忘掉,今天记下来. html: <input type="fi ...
- Asp.net上传文件Request.files获取不到文件
使用ftp上传文件,并且Request.files获取文件,今天发现获取到的文件个数始终是0个,查了下原来form标签中需加入enctype=”multipart/form-data”,呵呵了 < ...
- flask上传文件时request.files为空的解决办法
在做上传文件的时候遇到request.files是空 原因在于html中的表单form没有指明 enctype="multipart/form-data" <form met ...
- 多选文件批量上传前端(ajax*formdata)+后台(Request.Files[i])---input+ajax原生上传
1.配置Web.config;设定上传文件大小 <system.web> <!--上传1000M限制(https://www.cnblogs.com/Joans/p/4315411. ...
- MVC文件上传01-使用jquery异步上传并客户端验证类型和大小
本篇体验MVC上传文件,从表单上传过渡到jquery异步上传. MVC最基本的上传文件是通过form表单提交方式 □ 前台视图部分 <% using(Html.BeginForm("F ...
- MVC项目使用easyui的filebox控件上传文件
开发环境:WIN10+IE11,浏览器请使用IE10或以上版本 开发技术框架MVC4+JQuery Easyui+knockoutjs 效果为弹出小窗体,如下图 1.前端cshtml文件代码(只包含文 ...
- ASP.NET Core文件上传IFormFile于Request.Body的羁绊
前言 在上篇文章深入探究ASP.NET Core读取Request.Body的正确方式中我们探讨了很多人在日常开发中经常遇到的也是最基础的问题,那就是关于Request.Body的读取方式问题,看是简 ...
- java web(四):request、response一些用法和文件的上传和下载
上一篇讲了ServletContent.ServletCOnfig.HTTPSession.request.response几个对象的生命周期.作用范围和一些用法.今天通过一个小项目运用这些知识.简单 ...
随机推荐
- Asp.net Vnext TagHelpers
概述 本文已经同步到<Asp.net Vnext 系列教程 >中] TagHelpers 是vnext中引入的新功能之一.TagHelper 的作用是类似于发挥在以前版本的 ASP.NET ...
- 【PAT】1017 A除以B(20 分)
1017 A除以B(20 分) 本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数.你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立. 输入格式: 输入在一 ...
- 【洛谷】P4643 【模板】动态dp
题解 在冬令营上听到冬眠的东西,现在都是板子了猫锟真的是好毒瘤啊(雾) (立个flag,我去thusc之前要把WC2018T1乱搞过去= =) 好的,我们可以参考猫锟的动态动态dp的课件,然后你发现你 ...
- 【LOJ】#2035. 「SDOI2016」征途
题解 有人管它叫带权二分,有人管它叫dp凸优化,有人管它叫wqs二分-- 延伸出来还有zgl分治,xjp¥!%#!@#¥!# 当我没说 我们拆个式子,很容易发现所求的就是 \(m\sum_{i = 1 ...
- linux用户下的.profile文件丢失
登录用户时出现以下问题: #su - wqq-bash-4.1$ -bash-4.1$ 查看时发现环境变量文件丢失造成的 解决方法: # ls -la /etc/skel/ total 36drwx ...
- Tomcat --> Cannot create a server using the selected type
今天在eclipse想把之前的Tomcat 6删掉,重新配置一个,不料没有下一步 Cannot create a server using the selected type 这句话出现在窗口上面,应 ...
- mysql store procedure 存储过程
参考资料: 1.http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html 2.https://dev.mysql.com/doc/refman/5.7/ ...
- dSploitzANTI渗透教程之启动zANTI工具
dSploitzANTI渗透教程之启动zANTI工具 启动zANTI工具 [示例1-2]下面将介绍启动zANTI工具的方法.具体操作步骤如下所示: (1)在Android设备的应用程序界面,选择并启动 ...
- 1032 Sharing (25)(25 point(s))
problem To store English words, one method is to use linked lists and store a word letter by letter. ...
- 1024 Palindromic Number (25)(25 point(s))
problem A number that will be the same when it is written forwards or backwards is known as a Palind ...