后台代码:

using System;
using System.Data;
using System.Configuration;
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;
using System.Collections;

 

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }

    /// <summary>
    /// 增加按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ArrayList arraylist = new ArrayList();

        if (Session["UPLOAD"] == null)
        {
            //检查是否有文件要上传
            if (this.FileUpload1.HasFile)
            {
                if (this.FileUpload1.PostedFile.ContentLength > 4096000)
                {
                    lblResult.Text = "文件不能超过4M!";
                    return;
                }
                else
                {
                    lblResult.Text = "";
                }

                //将要上传的文件存入ArrayList和Session中
                arraylist.Add(this.FileUpload1.PostedFile);
                Session["UPLOAD"] = arraylist;
            }
            else
            {
                lblResult.Text = "请选择上传文件!";
            }
        }
        else if (Session["UPLOAD"] != null)
        {
            //检查是否有文件要上传
            if (this.FileUpload1.HasFile)
            {
                if (this.FileUpload1.PostedFile.ContentLength > 4096000)
                {
                    lblResult.Text = "文件不能超过4M!";
                    return;
                }
                else
                {
                    lblResult.Text = "";
                }
                arraylist = Session["UPLOAD"] as ArrayList;

                //将要上传的文件存入ArrayList和Session中
                arraylist.Add(this.FileUpload1.PostedFile);
                Session["UPLOAD"] = arraylist;
            }
            else
            {
                lblResult.Text = "请选择上传文件!";
            }
        }

        //绑定DataList
        DataList1.DataSource = Session["UPLOAD"];
        DataList1.DataBind();
    }

    /// <summary>
    /// 上传按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param> n,
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string strPath = Server.MapPath("~/files/");

        if (Session["UPLOAD"] != null)
        {
            //从ArrayList中取出要上传的文件
            ArrayList list = Session["UPLOAD"] as ArrayList;
            for (int i = 0; i < list.Count; i++)
            {
                HttpPostedFile PostedFile = list[i] as HttpPostedFile;

                //将文件上传到指定路径
                PostedFile.SaveAs(strPath + System.IO.Path.GetFileName(PostedFile.FileName));
            }
            lblResult.Text = "上传成功!";
        }

        //上传后清空上传文件列表
        if (CheckBox1.Checked)
        {
            Session["UPLOAD"] = null;
            DataList1.DataSource = Session["UPLOAD"];
            DataList1.DataBind();
        }
    }
}

 

前台代码:

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

 

<!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>
</head>
<body>
<center>
<h2>
    &nbsp;</h2>
    <form id="form1" runat="server">
    <div>
        &nbsp;
   
        <asp:Label ID="Label1" runat="server" Style="font-weight: bold; font-size: x-large"
            Text="文件上传操作示例"></asp:Label>
   
    </div>
        <table border="1" style="width: 676px; height: 197px">
            <tr>
                <td colspan="3">
                    <asp:FileUpload ID="FileUpload1" runat="server" Width="404px" Height="29px" /></td>
            </tr>
            <tr>
                <td colspan="3" rowspan="2">
                    <asp:Label ID="lblResult" runat="server" ForeColor="Red"></asp:Label></td>
            </tr>
            <tr>
            </tr>
            <tr>
                <td colspan="2" rowspan="1" style="width: 2503px; height: 25px;" align="center">
                    <asp:Button ID="btnAdd" runat="server" Text="增 加" OnClick="btnAdd_Click" /></td>
                <td colspan="1" rowspan="1" align="center" style="height: 25px">
                    &nbsp;<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上 传" /></td>
            </tr>
            <tr>
                <td align="center" colspan="3" rowspan="1">
                    <asp:CheckBox ID="CheckBox1" runat="server" Text="上传后清空上传文件列表" AutoPostBack="True" /></td>
            </tr>
            <tr>
                <td colspan="3" rowspan="2">
                    <asp:DataList ID="DataList1" runat="server" CellPadding="3" Height="1px" Width="615px" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" GridLines="Horizontal">
                        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                        <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <AlternatingItemStyle BackColor="#F7F7F7" />
                        <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <ItemTemplate>
                            &nbsp;
                            <table style="width: 419px; height: 54px">
                                <tr>
                                    <td>
                                        文件名称:</td>
                                    <td style="width: 288px">
                            <asp:Label ID="lblName" runat="server" Text='<%# Eval("FileName") %>'></asp:Label></td>
                                </tr>
                                <tr>
                                    <td>
                                        文件大小:</td>
                                    <td style="width: 288px">
                                        <asp:Label ID="lblLength" runat="server" Text='<%# Eval("ContentLength") %>'></asp:Label></td>
                                </tr>
                                <tr>
                                    <td style="height: 20px">
                                        文件类型:</td>
                                    <td style="width: 288px; height: 20px;">
                                        <asp:Label ID="lblType" runat="server" Text='<%# Eval("ContentType") %>'></asp:Label></td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <HeaderTemplate>
                            上传文件列表
                        </HeaderTemplate>
                    </asp:DataList>&nbsp;</td>
            </tr>
            <tr>
            </tr>
        </table>
    </form>
    </center>
</body>
</html>

posted on 2010-12-01 15:42  露水丛生  阅读(2317)  评论(3编辑  收藏  举报