多檔案上傳

來源:互聯網
上載者:User
上傳

---StyleSheet.css

.bluebutton
{
 background-color:LightSteelBlue;
 border-style:solid;
 border-width: 1px;
 border-color: LightSkyBlue;
}

---Attachme.aspx

<%@ Page language="c#" Codebehind="Attachme.aspx.cs" AutoEventWireup="false" Inherits="UploadFile.Attachme" %>
<HTML>
 <HEAD>
  <title>多檔案上傳</title>
  <LINK href="StyleSheet.css" rel="stylesheet">
 </HEAD>
 <body>
  <form id="attachme" method="post" encType="multipart/form-data" runat="server">
   <INPUT class="bluebutton" 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="bluebutton" Height="23px" Width="72px" Text="添加檔案"></asp:button>
   <asp:button id="RemvFile" runat="server" CssClass="bluebutton" Height="23px" Width="72px" Text="刪除檔案"></asp:button>
   <INPUT class="bluebutton" 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>

---Attachme.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UploadFile
{
 /// <summary>
 /// 多檔案上傳
 /// </summary>
 public class Attachme : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button AddFile;
  protected System.Web.UI.WebControls.Button RemvFile;
  protected System.Web.UI.HtmlControls.HtmlInputFile FindFile;
  protected System.Web.UI.HtmlControls.HtmlInputButton Upload;
  protected System.Web.UI.HtmlControls.HtmlGenericControl txtOutput;
  protected System.Web.UI.WebControls.ListBox FileList;
  protected System.Web.UI.WebControls.Label TipInfo;

  static public ArrayList hif = new ArrayList(); // 儲存檔案清單
  public int filesUploaded = 0; // 上傳檔案的數量
   
  private void Page_Load(object sender, System.EventArgs e)
  {
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {   
   InitializeComponent();
   base.OnInit(e);
  }
       
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {   
   this.AddFile.Click += new System.EventHandler(this.AddFile_Click);
   this.RemvFile.Click += new System.EventHandler(this.RemvFile_Click);
   this.Upload.ServerClick += new System.EventHandler(this.Upload_ServerClick);
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion

  /// <summary>
  /// 將要上傳的檔案添加到listbox中
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void AddFile_Click(object sender, System.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>
  private void RemvFile_Click(object sender, System.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();
   }
  }
 }
}

效果圖






相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。