---
title: "在ASP.NET中实现多文件上传（转）"
author: "cetusz"
date: "2010-04-05T11:10:00Z"
tags: []
categories: ["编程语言"]
slug: "asp-net"
draft: false
lastmod: "2010-10-18T09:30:20Z"
id: 787124
url: "https://cetus.iteye.com/blog/787124"
---

ASPX 代码

&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiFileUpload.aspx.cs"  
  Inherits="MultiFileUpload" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;  
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;  
&lt;head runat="server"&gt;  
  &lt;title&gt;孟宪会多文件上传测试&lt;/title&gt;

  &lt;script type="text/javascript"&gt;  
    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);  
    }  
  &lt;/script&gt;

&lt;/head&gt;  
&lt;body&gt;  
  &lt;form id="form1" runat="server" method="post" enctype="multipart/form-data"&gt;  
  &lt;h3&gt;多文件上传&lt;/h3&gt;  
   用户名：&lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;  
  &lt;div id="\_container"&gt;  
    &lt;input type="file" size="50" name="File" /&gt;  
  &lt;/div&gt;  
  &lt;div&gt;  
    &lt;input type="button" value="添加文件(Add)" onclick="addFile()" /&gt;  
  &lt;/div&gt;  
  &lt;div style="padding:10px 0"&gt;  
    &lt;asp:Button runat="server" Text="开始上传" ID="UploadButton"  
      onclick="UploadButton\_Click"&gt;&lt;/asp:Button&gt;  
  &lt;/div&gt;  
  &lt;div&gt;  
    &lt;asp:Label ID="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"  
      Width="500px" BorderStyle="None" BorderColor="White"&gt;&lt;/asp:Label&gt;  
  &lt;/div&gt;  
  &lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;

 

C# 代码

&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;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 + "&lt;br/&gt;");  
      strMsg.Append("上传的文件分别是：&lt;hr color='red'/&gt;");  
      try  
      {  
        for (int iFile = 0; iFile &lt; 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() + "&lt;br&gt;");  
            strMsg.Append("客户端文件地址：" + postedFile.FileName + "&lt;br&gt;");  
            strMsg.Append("上传文件的文件名：" + fileName + "&lt;br&gt;");  
            strMsg.Append("上传文件的扩展名：" + fileExtension + "&lt;br&gt;&lt;hr&gt;");  
            ///'可根据扩展名字的不同保存到不同的文件夹  
            ///注意：可能要修改你的文件夹的匿名写入权限。  
            postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);  
          }  
        }  
        strStatus.Text = strMsg.ToString();  
      }  
      catch (System.Exception Ex)  
      {  
        strStatus.Text = Ex.Message;  
      }

    }  
}