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几个对象的生命周期.作用范围和一些用法.今天通过一个小项目运用这些知识.简单 ...
随机推荐
- Java--Jackson转换Date,Timestamp 到格式化字符串
package com.diandaxia.test; import java.sql.Timestamp; import java.util.Date; /** * Created by del-b ...
- HDU 3374 String Problem(KMP+最大(最小)表示)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:给出一个字符串,依次左移一个单位形成一堆字符串,求其字典序最小和最大的字符串需要左移多 ...
- mybatis中多条件判断---choose when的用法
<select id="getFunctionByPage" resultMap="FunctionRlt"> SELECT K.FUNCTION_ ...
- php安装amqp扩展
1.要安装AMQP PHP扩展,必须先安装librabbitmq库 1.1使用以下步骤下载并安装库: # 下载 git clone git://github.com/alanxz/rabbitmq-c ...
- WORDPRESS登录后台半天都无法访问或者是访问慢的解决方法
wordpress登录后台如果打开速度慢,一般分为两部分,第一部分是php虚拟主机的原因,其中主机的原因,又分为很多种情况.第二部分就是WordPress程序本身的问题.这里无忧主机小编主要是讲第二部 ...
- Python并发编程-IO模型-非阻塞IO实现SocketServer
Server.py import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.setblocking(False) #把soc ...
- poj-2253-poj-1797_最短路练习
title: poj-2253-poj-1797_最短路练习 date: 2018-11-17 11:48:51 tags: acm 刷题 categories: ACM-最短路 概述 一道最短路的变 ...
- RMQ_第一弹_Sparse Table
title: RMQ_第一弹_Sparse Table date: 2018-09-21 21:33:45 tags: acm RMQ ST dp 数据结构 算法 categories: ACM 概述 ...
- @NamedEntityGraphs --JPA按实体类对象参数中的字段排序问题得解决方法
JPA按实体类对象参数中的字段排序问题得解决方法@Entity @Table(name="complaints") @NamedEntityGraphs({ @NamedEntit ...
- [POI2000]病毒 --- AC自动机
[POI2000]病毒 题目描述: 二进制病毒审查委员会最近发现了如下的规律: 某些确定的二进制串是病毒的代码. 如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的. 现在委员会已经找 ...