上传多个文件问题

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

<!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>Untitled Page</title>
</head>
<body>
   
   <form id="UPLOAD" method="post" runat="server" enctype="multipart/form-data">
    <h3>Multiple File Upload Example</h3>
     <input id="File1" type="file" runat="server" size="50"/>
    <p>
     <input id="File2" type="file" runat="server" size="50"/></p>
    <p>
     <input id="File3" type="file" runat="server" size="50"/></p>
    <p>
     <input id="File4" type="file" runat="server" size="50"/></p>
    <p>
     <input id="File5" type="file" runat="server" size="50"/></p>
    
     <asp:LinkButton id="LinkButton1" runat="server" Font-Names="Verdana" Font-Bold="True" Font-Size="XX-Small">Upload Images</asp:LinkButton>&nbsp;&nbsp;<STRONG>::
     </STRONG>&nbsp; <A href="JavaScript:document.forms[0].reset()" id="LinkButton2" style="FONT-WEIGHT:bold;FONT-SIZE:xx-small;FONT-FAMILY:verdana">
      Reset Form</A>&nbsp
    <p>
     <asp:Label id="Label1" runat="server" Font-Names="verdana" Font-Bold="True" Font-Size="XX-Small" Width="400px" BorderStyle="None" BorderColor="White"></asp:Label></P>
    <p>&nbsp;</p>
   </form>
  
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class pageload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
            this.SaveImages();
    }
    #region
    private System.Boolean SaveImages()
    {
        //loop through the files uploaded

        System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;

        //Message to the user
        System.Text.StringBuilder _message = new System.Text.StringBuilder("Files Uploaded:<br>");

        try
        {
            for (System.Int32 _iFile = 0; _iFile < _files.Count; _iFile++)
            {

                // Check to make sure the uploaded file is a jpg or gif

                System.Web.HttpPostedFile _postedFile = _files[_iFile];
                System.String _fileName, _fileExtension;

                _fileName = System.IO.Path.GetFileName(
                _postedFile.FileName);

                _fileExtension = System.IO.Path.GetExtension(
                    _fileName);

                if (_fileExtension == ".gif")
                {

                    //Save File to the proper directory
                    _postedFile.SaveAs(
                        System.Web.HttpContext.Current.Request.MapPath(
                        "gifs/") + _fileName);
                    _message.Append(_fileName + "<BR>");

                }
                else if (_fileExtension == ".jpg")
                {

                    //Save File to the proper directory    
                    _postedFile.SaveAs(
                        System.Web.HttpContext.Current.Request.MapPath(
                        "jpgs/") + _fileName);
                    _message.Append(_fileName + "<BR>");

                }
                else
                {

                    _message.Append(_fileName + " <font color=/"red/">failed!! Only .gif and .jpg images allowed!</font> <BR>");

                }

            }

            Label1.Text = _message.ToString();
            return true;
        }
        catch (System.Exception Ex)
        {

            Label1.Text = Ex.Message;
            return false;

        }

    }
    #endregion

    #region Web Form Designer generated code
    override protected void OnInit(System.EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        InitializeComponent();
        base.OnInit(e);
    }
    #endregion

    #region Web Form Designer generated code
   

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值