多文件上传示例源码

1.在vs.2005中新建.aspx,

HTML代码如下: 

<head id="Head1" runat="server">
    <title>多文件上传示例源码</title>
    <style type="text/css">
    body
    {
    font-size:12px;
    }
    aspx_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">
        多文件上传示例<br />
        <input id="FindFile" type="file" size="28" 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="aspx_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="129px" Width="273px"></asp:Label>
</body>
</html>

2.编写.cs代码,如下:

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    static public ArrayList hif = new ArrayList(); // 保存文件列表
    public int filesUploaded = 0; // 上传文件的数量

    /// <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("Files/"); // 上传路径  
        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();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值