<%@ 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>孟宪会多文件上传测试</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:TextBoxID="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:Buttonrunat="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>

C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

publicpartialclass MultiFileUpload : System.Web.UI.Page
{
    protectedvoid 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;
      }

}
}

下载

public void Download(string path) //要下载文件的名称
{  比如string path = "\\images\\a.text";自己检查自己的路径问题,有的需要添加上级目录 有的直接输入文件名就可
       if (!string.IsNullOrEmpty(path))    
       {            
          string filePath = Server.MapPath("")+path;//路径       
          // string filePath =@"C:\Users\Public\Pictures\Sample Pictures\1.jpg";
           FileInfo fileInfo = new FileInfo(filePath);        
           Response.Clear();               
           Response.ClearContent();          
           Response.ClearHeaders();              
           Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);   
           Response.AddHeader("Content-Length", fileInfo.Length.ToString());                 
           Response.AddHeader("Content-Transfer-Encoding", "binary");               
           Response.ContentType = "application/octet-stream";                  
           Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
           Response.WriteFile(fileInfo.FullName);            
           Response.Flush();              
           Response.End();            
       }
}

aspx 文件上传和下载,多文件上传的更多相关文章

  1. XShell上传、下载本地文件到linux服务器

    Python之道发表于程序员八阿哥订阅 1.2K 腾讯云服务器 年付3折起 首次购买云服务器 最低3折起 超高性价比 限时抢购 Xshell很好用,然后有时候想在windows和linux上传或下载某 ...

  2. php 上传文件实例 上传并下载word文件

    上传界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  3. Eclipse 上传 删除 下载 分析 hdfs 上的文件

    本篇讲解如何通过Eclipse 编写代码去操作分析hdfs 上的文件. 1.在eclipse 下新建Map/Reduce Project项目.如图:  项目建好后,会默认加载一系列相应的jar包. 下 ...

  4. Linux命令行上传本地文件到服务器 、 下载服务器文件到本地

    sh使用命令: scp 将本地文件上传至服务器 第一个是本地文件的路径/文件名, 例如 ./index.tar.gz  . index.html . bg.png 等 第二个是要上传到的服务器的位置  ...

  5. SpringMVC下文件的上传与下载以及文件列表的显示

    1.配置好SpringMVC环境-----SpringMVC的HelloWorld快速入门! 导入jar包:commons-fileupload-1.3.1.jar和commons-io-2.4.ja ...

  6. linux学习 XShell上传、下载本地文件到linux服务器

    (一)通过命令行的方式 1.linux服务器端设置 在linux主机上,安装上传下载工具包rz及sz; 如果不知道你要安装包的具体名称,可以使用yum provides */name 进行查找系统自带 ...

  7. drupal7 开发自定义上传、下载模块的上传功能

    关键点有两个:1.在页面上显示出上传的控件,2.代码实现文件上传到服务器的功能 一.显示控件: 先来看关键点1: 实现页面显示出上传控件, 关键代码: $form['my_file_field'] = ...

  8. .NET环境下上传和下载Word文件

    一.上传Word文档或者其他文档 1.简单地上传文件的web服务方法如下 [WebMethod] public void UploadFile() { using (TransactionScope ...

  9. 使用fastDFS上传和下载图片文件

    package com.xuecheng.test.fastdfs;import org.csource.common.MyException;import org.csource.fastdfs.* ...

  10. Java利用 ganymed-ssh2-build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令

    package api; import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOExcepti ...

随机推荐

  1. android开发注意点

    一.Android编码规范 1.java代码中不出现中文,最多注释中可以出现中文 2.局部变量命名.静态成员变量命名 只能包含字母,单词首字母出第一个外,都为大写,其他字母都为小写 3.常量命名 只能 ...

  2. JavaScript高级程序设计:第十四章

    第十四章 一.表单的基础知识 在HTML中,表单是由<form>元素来表示的,而在javascript中,表单对应的则是HTMLFormElement类型.HTMLFormElement继 ...

  3. JavaScript高级程序设计:第五章

    引用类型 一.object类型: 创建object实例的方式有两种.第一种是使用new操作符后跟Object构造函数,如下所示: var  person = new  Object(): person ...

  4. hdu_5778_abs(暴力)

    题目链接:hdu_5778_abs 题意: 给你一个数x,然你找一个y,这个y只能被素数分解,每一个素数恰好出现2次,求y-x的绝对值最小 题解: 给官方的  ps:数据太水,打35个素数也能过 #i ...

  5. spice-vdagent

    The spice-vdagent should be running in the guest. Have you installed the spice guest tools in your w ...

  6. UML示例图

  7. 使用oracle数据库开发,异常总结

    最近两天使用的oracle数据库开发项目时遇到了2个异常,第一个是执行sql语句时报异常:“ORA-00911: 无效字符”,如下图: sql语句如下: 断点调试,把sql语句拷贝到pl/sql里执行 ...

  8. Spring中实现监听的方法

    在未使用框架进行编程的时候,我们常常在web.xml中加上这样一段话 <listener> <listener-class>XXX</listener-class> ...

  9. libmysql.dll 找不到

    在用C#开发的时候,需要连接MySQL  ,系统提示  libmysql.dll 找不到模块. 我们可以找到 MySQL安装文件夹下的 C:\Program Files\MySQL\MySQL Ser ...

  10. math ceil以及math floor,math:round

    ◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数:◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数:◎Math.round()执行标准舍入,即它总 ...