Asp.Net多文件上传

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_1aspxUploads._Default" %>

<!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>51aspx多文件上传示例源码</title>
    <style>
    body
    {
    font-size:12px;
    }
    .51aspx_Button
    {
     background-color:LightSteelBlue;
     border-style:solid;
     border-width: 1px;
     border-color: LightSkyBlue;
    }


    </style>
</head>
<body>
  <form id="attachme" method="post" encType="multipart/form-data" runat="server">
      51aspx 多文件上传示例<br />
      <br />
   <INPUT id="FindFile" type="file" size="26" runat="server" NAME="FindFile">
   <BR>
   <asp:listbox id="FileList" runat="server" CssClass="txtbox" Height="100px" Width="274px" SelectionMode="Multiple"></asp:listbox><BR>
   <asp:button id="AddFile" runat="server" CssClass="51aspx_Button" Height="23px" Width="72px" Text="添加文件" OnClick="AddFile_Click"></asp:button>
   <asp:button id="RemvFile" runat="server" CssClass="51aspx_Button" Height="23px" Width="72px" Text="删除文件" OnClick="RemvFile_Click"></asp:button>
   <INPUT class="51aspx_Button" id="Upload" type="submit" value="上传" runat="server" onserverclick="Upload_ServerClick"
    NAME="Upload">
  </form>
  <asp:label id="TipInfo" runat="server" Height="25px" Width="249px"></asp:label>

</body>
</html>

 

.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace _1aspxUploads
{
    //51aspx.com多文件上传示例
    public partial class _Default : System.Web.UI.Page
    {
        static public ArrayList hif = new ArrayList(); // 保存文件列表
        public int filesUploaded = 0; // 上传文件的数量

        protected void Page_Load(object sender, EventArgs e)
        {

        }
       /// <summary>
        /// 将要上传的文件添加到listbox中
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>

        protected void AddFile_Click(object sender, EventArgs e)
        {
            if (Page.IsPostBack == true)
            {
                hif.Add(FindFile);
                FileList.Items.Add(FindFile.PostedFile.FileName);
            }
            else
            { }

        }

        /// <summary>
        /// 从listbox中删除指定的文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RemvFile_Click(object sender, EventArgs e)
        {
            if (FileList.SelectedIndex == -1)
            {
                TipInfo.Text = "错误 - 必须指定要删除的文件.";
                return;
            }
            else if (FileList.Items.Count != 0)
            {
                hif.RemoveAt(FileList.SelectedIndex);
                FileList.Items.Remove(FileList.SelectedItem.Text);
                TipInfo.Text = "";
            }          

        }

          /// <summary>
  /// 循环上传listbox中的文件到指定的文件夹下
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
        public void Upload_ServerClick(object sender, System.EventArgs e)
        {
            string baseLocation = Server.MapPath("UploadFiles/"); // 上传路径  
            string status = "";  // 上传成功后显示的文件列表        

            if ((FileList.Items.Count == 0) && (filesUploaded == 0))
            {
                TipInfo.Text = "错误 - 必须指定要上传的文件.";
                return;
            }
            else
            {
                foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
                {
                    try
                    {
                        string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
                        HIF.PostedFile.SaveAs(baseLocation + fn);
                        filesUploaded++;
                        status += fn + "<br>";
                    }
                    catch (Exception err)
                    {
                        TipInfo.Text = "上传错误 " + baseLocation
                         + "<br>" + err.ToString();
                    }
                }

                if (filesUploaded == hif.Count)
                {
                    TipInfo.Text = "共上传了 " + filesUploaded + " 个文件。 <br>" + status;
                }
                hif.Clear();
                FileList.Items.Clear();
            }

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值