<%@ 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. 好玩的获取目录信息的例子[C#]

    DirectoryInfo dirinfo = new DirectoryInfo("d:\\111"); DirectoryInfo[] dirs = dirinfo.GetDi ...

  2. Robotium第一天:搭建环境测试微信

    因为要在命令行下运行一些android的工具,所以配置一些环境变量会比较方便: 遇到问题: java -jar re-sign.jar 出现提示android路径没有配置好: 需要配置如下: 配置AN ...

  3. MYSQL超时连接问题(com.mysql.jdbc.MysqlIO.readFully)

    应用服务器连接mysql,有时候会出现以下异常: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.jav ...

  4. html 细线表格

    可以<table width="800px" cellpadding="0" border="1px" style="bor ...

  5. 微信接口开发1--向微信发送请求--获取access_token

    //随便放置一个php文件在服务器上.执行该方法--调用模拟get提交---到微信-->获得微信返回的access_token 不建议自己编写模拟get提交方法. 建议直接导入微信框架LaneW ...

  6. HDU1711:Number Sequence

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...

  7. c#操作oracle的通用类

    using System;using System.Collections;using System.Collections.Generic;using System.Data;using Syste ...

  8. 萝卜德森的sublime笔记中文翻译版

    我已经使用subliem编辑器版本2接近2个月了,并且我在其中找到了一堆有用的技巧.我发觉应该写下这些技巧,为那些对此感兴趣的人们.我会尽力的详细描述,那些看起来像魔法一样的东西,因为很多非常“酷”的 ...

  9. Entity Framework 学习高级篇2—改善EF代码的方法(下)

    ,IQueryable<Customers>>( (database) => database.Customers.Where(c => c.City == " ...

  10. dp + 组合数 Codeforces Beta Round #9 (Div. 2 Only) D

    http://codeforces.com/problemset/problem/9/D 题目大意:给你一个二叉树和n个数字,满足左小右大,能形成多少种不同的二叉树 思路:定义dp[i][j]表示目前 ...