<%@ 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. iosOC可变数组选择,冒泡排序

    #pragma mark 可变数组的排序 NSMutableArray * array = [NSMutableArray arrayWithObjects: @"1",@&quo ...

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

    一.理解包含不同层次节点的DOM 1.节点层次 以下面的HTML为例: <html> <head> <title>Sample Page</title> ...

  3. postgres-xl 集体搭建(2)

    $ pgxc_ctl prepare config minimal 生成一个单机伪分布集群配置简单模板 根据该模板进行简单修改就可以配置成一个集群 $ pgxc_ctl init all $ pgxc ...

  4. Perl 之 use(), require(), do(), %INC and @INC

    来源: http://www.cnblogs.com/itech/archive/2013/03/12/2956185.html 转自:http://perl.apache.org/docs/gene ...

  5. iOS 10推送通知开发

    原文地址:Developing Push Notifications for iOS 10,译者:李剑飞 虽然通知经常被过度使用,但是通知确实是一种获得用户关注和通知他们需要更新或行动的有效方式.iO ...

  6. 扫描局域网内的ip和主机名

    1. 目的 今天发现我配置的一台电脑ip被人占用了,所以准备写个程序扫描一下局域网内所有正在使用的ip和主机名 2. 实现--直接上代码 import time import threading im ...

  7. Lua基础(一)

    1.Lua中有8个基本类型分别为:nil.boolean.number.string.userdata.function.thread和table 2.Lua中的表达式包括数字常量.字符串常量.变量. ...

  8. Cookie 的设置和获取

    获取:var userName = getCookieValue("userName"); 设置:setCookie("userName",equpid,24, ...

  9. BigDecimal加减乘除运算

    java.math.BigDecimal.BigDecimal一共有4个够造方法,让我先来看看其中的两种用法: 第一种:BigDecimal(double val)Translates a doubl ...

  10. 689D Friends and Subsequences RMQ+二分

    题目大意:给出两个数组,求第一个数组区间内的最大值和第二个区间内的最小值相同的区间有多少种. 题目思路:通过预处理(O(n*Logn))后,每次查询的时间复杂度为O(1),但是如果暴力查询O(n*n) ...