在Web应用程序开发中,避免不了要用到上传文件这个功能,但以前上传文件是个很麻烦的事,现在有了.NET,文件上传变得轻而易举。下面的这个例子实现了多文件上传功能。可以动态添加输入表单,上传的文件数量没有限制。代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiFileUpload.aspx.cs"

Inherits="MultiFileUpload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>ASP.NET多文件上传测试</title>

<script type="text/javascript">

function addFile() {

var div = document.createElement("div");

var f = document.createElement("input");

f.setAttribute("type", "file")

f.setAttribute("name", "File")

f.setAttribute("size", "50")

div.appendChild(f)

var d = document.createElement("input");

d.setAttribute("type", "button")

d.setAttribute("onclick", "deteFile(this)");

d.setAttribute("value", "移除")

div.appendChild(d)

document.getElementById("_container").appendChild(div);

}

function deteFile(o) {

while (o.tagName != "DIV") o = o.parentNode;

o.parentNode.removeChild(o);

}

</script>

</head>

<body>

<form id="form1" runat="server" method="post" enctype="multipart/form-data">

<h3>多文件上传</h3>

用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<div id="_container">

<input type="file" size="50" name="File" />

</div>

<div>

<input type="button" value="添加文件(Add)" onclick="addFile()" />

</div>

<div style="padding:10px 0">

<asp:Button runat="server" Text="开始上传" ID="UploadButton"

onclick="UploadButton_Click"></asp:Button>

</div>

<div>

<asp:Label ID="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"

Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>

</div>

</form>

</body>

</html>

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class MultiFileUpload : System.Web.UI.Page

{

protected void UploadButton_Click(object sender, EventArgs e)

{

///'遍历File表单元素

HttpFileCollection files = HttpContext.Current.Request.Files;

/// '状态信息

System.Text.StringBuilder strMsg = new System.Text.StringBuilder("您输入的用户名是:" + TextBox1.Text + "<br/>");

strMsg.Append("上传的文件分别是:<hr color='red'/>");

try

{

for (int iFile = 0; iFile < files.Count; iFile++)

{

///'检查文件扩展名字

HttpPostedFile postedFile = files[iFile];

string fileName, fileExtension;

fileName = System.IO.Path.GetFileName(postedFile.FileName);

if (fileName != "")

{

fileExtension = System.IO.Path.GetExtension(fileName);

strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");

strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");

strMsg.Append("上传文件的文件名:" + fileName + "<br>");

strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");

///'可根据扩展名字的不同保存到不同的文件夹

///注意:可能要修改你的文件夹的匿名写入权限。

postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);

}

}

strStatus.Text = strMsg.ToString();

}

catch (System.Exception Ex)

{

strStatus.Text = Ex.Message;

}

}

}

ASP.NET多文件上传实例的更多相关文章

  1. 使用NeatUpload控件实现ASP.NET大文件上传

    使用NeatUpload控件实现ASP.NET大文件上传 一般10M以下的文件上传通过设置Web.Config,再用VS自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不 ...

  2. ASP.NET Core文件上传IFormFile于Request.Body的羁绊

    前言 在上篇文章深入探究ASP.NET Core读取Request.Body的正确方式中我们探讨了很多人在日常开发中经常遇到的也是最基础的问题,那就是关于Request.Body的读取方式问题,看是简 ...

  3. PHP中,文件上传实例

    PHP中,文件上传一般是通过move_uploaded_file()来实现的.  bool move_uploaded_file ( string filename, string destinati ...

  4. PHP学习笔记--文件目录操作(文件上传实例)

    文件操作是每个语言必须有的,不仅仅局限于PHP,这里我们就仅用PHP进行讲解 php的文件高级操作和文件上传实例我放在文章的最后部分.--以后我还会给大家写一个PHP类似于网盘操作的例子 注意:阅读此 ...

  5. ASP.NET - 多文件上传,纯代码,不使用插件

    解决方案: 前段代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mu ...

  6. ASP.NET MVC 文件上传和路径处理

    ASP.NET MVC 文件上传和路径处理总结 目录 文件的上传和路径处理必须解决下面列出的实际问题: 1.重复文件处理 2.单独文件上传 3.编辑器中文件上传 4.处理文章中的图片路径 5.处理上传 ...

  7. ExtJS:文件上传实例

    ExtJS:文件上传实例 var ext_dateFormat = 'Y-m-d H:i:s'; var dateFormat = 'yyyy-MM-dd HH:mm:ss'; var date = ...

  8. ASP.NET MVC文件上传【转】

    最近用到了文件上传功能,下面给出ASP.NET MVC文件上传的一个简单示例: 一.前端代码 @using (Html.BeginForm("UploadFile", " ...

  9. [转载]ASP.NET Core文件上传与下载(多种上传方式)

    ASP.NET Core文件上传与下载(多种上传方式)   前言 前段时间项目上线,实在太忙,最近终于开始可以研究研究ASP.NET Core了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在 ...

随机推荐

  1. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  2. poj2253 Frogger(最短路变型或者最小生成树)

    /* 题意:就是源点到终点有多条的路径,每一条路径中都有一段最大的距离! 求这些路径中最大距离的最小值! Dijkstra, Floyd, spfa都是可以的!只不过是将松弛的条件变一下就行了! 想了 ...

  3. 让Team Foundation Server/TFS自动记住用户名密码解决方案

    在使用Team Foundation Server(以下简称TFS) 的时候,在每次打开Visual Studio TFS时候,需要输入用户名和秘密,比较麻烦.现提供一种方法可以解决这个问题: 依次执 ...

  4. CLR执行模型

    好好学习底层运行机制,从CLR via C# 开始. CLR的执行模型: CLR:Common Language Runtime,是一个可由多种编程语言使用的"运行时".CLR的核 ...

  5. Testing - 测试基础 - 模型

    珠玉在前,不再赘言. 软件测试模型 软件测试模型汇总

  6. 纯JS实现可拖拽表单

    转载注明出处!!! 转载注明出处!!! 转载注明出处!!! 因为要用到可拖拽表单,个人要比较喜欢自己动手,不怎么喜欢在不懂实现或者原理的情况下用插件,所以查找资料实现了一个. 思路:放入:用mouse ...

  7. 开启事务时mybatis返回主键id

    先说一下没有注解的 先给出实体类: public class City { private int city_id; private String city_name; public int getC ...

  8. Windows Azure Virtual Network (10) 使用Azure Access Control List(ACL)设置客户端访问权限

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的China Azure. 我们在创建完Windows Azure Virtual Machi ...

  9. Mybatis在idea中错误:Invalid bound statement (not found)

    学习mybatis的过程中,测试mapper自动代理的时候一直出错,在eclipse中可以正常运行,而同样的代码在idea中却无法成功.虽然可以继续调试,但心里总是纠结原因.百度了好久,终于找到一个合 ...

  10. SQL Server代理(4/12):配置数据库邮件

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 在以前的文章里我们看到,SQL Serve ...