c/s模式 (C#)下Ftp的多文件上傳及其上傳進度


因為項目要求,制作的一個多文件上傳,並顯示進度條一段代碼(vs2005環境)。
(只為粗略的實現,代碼並不規范)

當多個文件上傳的時候,需要依次隊列形式一個個上傳,當上傳某個文件的時候,鎖定進程,上傳完畢再開啟鎖。

在主類中的上傳按鈕事件代碼: 

     //獲取openFileDialog控件選擇的文件名數組(openFileDialog可多個文件選擇)
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            try
            {
                this.openFileDialog1.ShowDialog();
                path = this.openFileDialog1.FileNames;  //獲取openFileDialog控件選擇的文件名數組
                string strpath = "";
                for (int y = 0; y < path.Length; y++)
                {
                    strpath += path[y];
                }
                textBox1.Text = strpath;
            }
            catch
            {
                this.lbl_ftpStakt.Text = "請選擇文件!";
            }
        }

 

 
     //上傳按鈕事件
        private void button2_Click(object sender, EventArgs e)
        {
            this.lbl_ftpStakt.Visible = true;   //設置上傳信息標簽可見
            this.lbl_ftpStakt.Text = "連接服務器...";            
            
            try
            {  
                for (i = 0; i < path.Length; i++)
                {
                    filename = path[i].ToString();

                    //實例化事件類
                    myTest fo = new myTest(filename);
                    fo.startUpEvent+=new myTest.myUpEventsHandler(this.RunsOnWorkerThread); //注冊事件
                    fo.mythreadStart(); //調用類的方法
                   
                    FileInfo p = new FileInfo(path[i].ToString());
                    uploadSQL(p.Name);  //上傳到庫
                }
                //label1.Text = "上傳成功";
            }
            catch
            {
                string s="";
                for (int x = i; x < path.Length; x++)
                {
                    FileInfo file = new FileInfo(path[i].ToString());
                    s += file.Name + " ";
                }
                    this.lbl_ftpStakt.Text = "上傳失敗";
                MessageBox.Show(s.ToString()+" 上傳失敗","提示");
            }
        }
     //連接ftp上傳
        public void RunsOnWorkerThread(string _filename)
        {
            //阻塞線程
            mt.WaitOne();
            Interlocked.Increment(ref flag);    //狀態值+1

            this.lbl_ftpStakt.Text = "連接服務器中...";
            FileInfo fileInf = new FileInfo(_filename);
            FtpWebRequest reqFTP;
            // 根據uri創建FtpWebRequest對象 
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://210.82.***.***/" + fileInf.Name));
            // ftp用戶名和密碼
            reqFTP.Credentials = new NetworkCredential("record", "files");
            // 默認為true,連接不會被關閉
            // 在一個命令之后被執行
            reqFTP.KeepAlive = false;
            // 指定執行什么命令
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            // 指定數據傳輸類型
            reqFTP.UseBinary = true;
            // 上傳文件時通知服務器文件的大小
            reqFTP.ContentLength = fileInf.Length;
            //long _length = fileInf.Length;  /////////
            // 緩沖大小設置為2kb
            int buffLength = 2048;  ////
            byte[] buff = new byte[buffLength];
            int contentLen;
            // 打開一個文件流 (System.IO.FileStream) 去讀上傳的文件
            FileStream fs = fileInf.OpenRead();

            try
            {
                // 把上傳的文件寫入流
                Stream strm = reqFTP.GetRequestStream();
                // 每次讀文件流的2kb
                contentLen = fs.Read(buff, 0, buffLength);
                int allbye = (int)fileInf.Length;
                int startbye = 0;
                this.myProgressControl.Maximum = allbye;
                this.myProgressControl.Minimum = 0;
                this.myProgressControl.Visible = true;
                this.lbl_ftpStakt.Visible = true;
                this.lbl_ftpStakt.Text = "服務器連接中...";
                // 流內容沒有結束
                while (contentLen != 0)
                {
                    // 把內容從file stream 寫入 upload stream
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                    startbye += contentLen;
                    this.lbl_ftpStakt.Text = "已上傳:" + (int)(startbye / 1024) + "KB/" + "總長度:" + (int)(allbye / 1024) + "KB" + " " + " 文件名:" + fileInf.Name;
                    myProgressControl.Value = startbye;
                }
                // 關閉兩個流
                strm.Close();
                fs.Close();
                this.myProgressControl.Visible = false;
                this.lbl_ftpStakt.Text = "上傳成功!";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Upload Error");
            }
            Interlocked.Decrement(ref flag);
            mt.ReleaseMutex();//釋放線程
        }

 

處理上傳線程的委托事件類

   /// <summary>
    /// 委托事件類
    /// </summary>
    class myTest
    {
        public string filename;
        public delegate void myUpEventsHandler(string _filename);
        public event myUpEventsHandler startUpEvent;

        public myTest()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_filename">上傳的文件名</param>
        public myTest(string _filename)
        {
            this.filename = _filename;
        }

        /// <summary>
        /// 開始一個線程,執行事件
        /// </summary>
        public void mythreadStart()
        {
            Thread thr = new Thread(new ThreadStart(this.mystart));
            thr.Start();
        }

        /// <summary>
        /// 開始事件
        /// </summary>
        public void mystart()
        {
            startUpEvent(this.filename);
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM