您的位置:威尼斯官方网站 > 威尼斯正规官网 >  /// 多文件上传

 /// 多文件上传

发布时间:2020-01-04 13:44编辑:威尼斯正规官网浏览(113)

    ---StyleSheet.css

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

    ---Attachme.aspx

     

      

    多文件上传

             

           

       

       

       

           NAME="Upload">   

        

    ---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
    {
     ///
     /// 多文本上传
     ///
     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);
      }
           
      ///
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      ///
      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

      ///
      /// 就要上传的文书增加到listbox中
      ///
      ///
      ///
      private void AddFile_Click(object sender, System.EventArgs e)
      {
       if (Page.IsPostBack == true)
       {
        hif.Add(FindFile);
        FileList.Items.Add(FindFile.PostedFile.FileName);
       }
       else
       {}
      }

      ///
      /// 从listbox中剔除钦命的文件
      ///
      ///
      ///
      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 = "";
       }          
      }

      ///
      /// 循环上传listbox中的文件到钦定的文本夹下
      ///
      ///
      ///
      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 + "
    ";
         }
         catch(Exception err)
         {
          TipInfo.Text = "上传错误 " + baseLocation
           + "
    " + err.ToString();
         }
        }

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

    效果图

    图片 1

    图片 2

    <> <>

    本文由威尼斯官方网站发布于威尼斯正规官网,转载请注明出处: /// 多文件上传

    关键词: