posts - 124,  comments - 29,  trackbacks - 0

ftp上传或下载过程中或提供一个事件,当上传一定比特流的时候就触发该事件,上传的比特数可以在此事件处理函数中处理,上传结束也会触发一个事件。
**********************************UpLoadFtp类************************************************
{
           private FTPClient ftp;
            ftp.BytesTransferred += new BytesTransferredHandler(ftp_BytesTransferred);
            ftp.TransferCompleteEx += new TransferHandler(ftp_TransferCompleteEx);


 public void MyUpload(String remotePath,String localfilepath,String remotefilename)
{
          
            MyConnect();
          
             try
            {
                ftp.ChDir(remotePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"提示信息");
            }
           
             try
            {
                FileInfo fi = new FileInfo(localfilepath);

                ftp.TransferNotifyInterval = (int)((double)fi.Length / (double)100);

                ftp.Put(localfilepath,remotefilename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示信息");
            }
}




        void ftp_TransferCompleteEx(object sender, TransferEventArgs e)
        {
            TransferComplete.Invoke(sender, e);
        }

 

        public event BytesTransferredHandler BytesTransferred;
        public event TransferHandler TransferComplete;

        void ftp_BytesTransferred(object sender, BytesTransferredEventArgs e)
        {
            BytesTransferred.Invoke(sender, e);
        }
}


**************************调用上面类里的公有事件本类中加事件处理函数**************************
        uploadFtp.BytesTransferred += new EnterpriseDT.Net.Ftp.BytesTransferredHandler(uploadFtp_BytesTransferred);
        uploadFtp.TransferComplete += new EnterpriseDT.Net.Ftp.TransferHandler(uploadFtp_TransferComplete);
        void uploadFtp_TransferComplete(object sender, EnterpriseDT.Net.Ftp.TransferEventArgs e)
        {
            if (this.checkIsFormat.Checked)
            {
                Random ran = new Random();
                this.fileNewName = curtype + "#" + cursuppler + "#" + curnetName + "#"
                                   + comboFileType.SelectedItem.ToString()
                                   + "#" + DateTime.Now + "#" + ran + "#"
                                   + "1.src";
                uploadFtp.ReName(remoteFileName, fileNewName);
            }
            progressBar1.Value = 0;
            lbFtpMessage.ForeColor = Color.Red;
            lbFtpMessage.Text = "上传成功!";
        }

        void uploadFtp_BytesTransferred(object sender, EnterpriseDT.Net.Ftp.BytesTransferredEventArgs e)
        {
            this.progressBar1.Value = (int)(100 *((float)e.ByteCount / (float)localFileLength));

            string str = String.Format("上传进度: {0}%", progressBar1.Value);
            lbFtpMessage.ForeColor = Color.Black;
            lbFtpMessage.Text = str;
            Application.DoEvents();
        }
************************************************************************************************

  1using System;
  2using System.Collections.Generic;
  3using System.Windows.Forms;
  4using EnterpriseDT.Net.Ftp;
  5using System.Text;
  6using System.IO;
  7
  8namespace BurEmluator
  9{
 10    class UpLoadFile
 11    {
 12        private String serverIp;
 13        private String username;
 14        private String password;
 15        private FTPClient ftp;
 16        private long transferNotifyInterval;
 17
 18        public long TransferNotifyInterval
 19        {
 20            get return transferNotifyInterval; }
 21            set { transferNotifyInterval = value; }
 22        }

 23
 24        public UpLoadFile(string sip, string uname, string pwd)
 25        {
 26            this.serverIp = sip;
 27            this.username = uname;
 28            this.password = pwd;
 29            ftp = new FTPClient();
 30            ftp.BytesTransferred += new BytesTransferredHandler(ftp_BytesTransferred);
 31            ftp.TransferCompleteEx += new TransferHandler(ftp_TransferCompleteEx);
 32        }

 33
 34        void ftp_TransferCompleteEx(object sender, TransferEventArgs e)
 35        {
 36            TransferComplete.Invoke(sender, e);
 37        }

 38
 39
 40
 41        public event BytesTransferredHandler BytesTransferred;
 42        public event TransferHandler TransferComplete;
 43
 44        void ftp_BytesTransferred(object sender, BytesTransferredEventArgs e)
 45        {
 46            BytesTransferred.Invoke(sender, e);
 47        }

 48       
 49        public void MyUpload(String remotePath,String localfilepath,String remotefilename)
 50        {
 51            连接
 54            进入远程目录
 64            上传
 78        }

 79        public void ReName(String upfilename,String newfilename)
 80        {
 81            try
 82            {
 83                ftp.Rename(upfilename, newfilename);
 84            }

 85            catch (Exception ex)
 86            {
 87                MessageBox.Show(ex.Message,"提示信息");
 88            }

 89        }

 90        private void MyConnect()
 91        {
 92            连接
103            登陆
113
114            ftp.ConnectMode = FTPConnectMode.PASV;
115            ftp.TransferType = FTPTransferType.BINARY;
116        }

117    }

118}


  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Text;
  7using System.IO;
  8using System.Xml;
  9using System.Threading;
 10using System.Windows.Forms;
 11
 12namespace BurEmluator
 13{
 14    public partial class DataUpload : Form
 15    {
 16
 17        private List<NeInfoLocal> m_NeList;
 18        public List<NeInfoLocal> NeList
 19        {
 20            get
 21            {
 22                return m_NeList;
 23            }

 24            set
 25            {
 26                m_NeList = value;
 27            }

 28        }

 29        private String ip;
 30        private String username;
 31        private String pwd;
 32        private String remotepath;
 33        private UpLoadFile uploadFtp;
 34
 35        private String curnetName;
 36        private  String curtype;
 37        private  String cursuppler;
 38
 39        private string remoteFileName;
 40        private long localFileLength;
 41        private string localFilePath;
 42        private string localFileName;
 43        private TreeNode curTN = null;
 44        private string fileNewName = null;
 45        private string isSucName = "0.src";
 46        private string fileType;
 47        private bool isRename = false;
 48        public DataUpload()
 49        {
 50            InitializeComponent();
 51        }

 52        private void InitTreeView(int order)
 53        {
 54            PublicUse pubfun = new PublicUse();
 55
 56            bool bResult = pubfun.InitMTreeView(Netree, NeList, order);
 57            if (!bResult)
 58            {
 59                MessageBox.Show("未取出网元信息,请稍候使用该功能!""局数据仿真系统");
 60                return;
 61            }

 62        }

 63
 64        private void DataUpload_Load(object sender, EventArgs e)
 65        {
 66            //网元
 67            InitTreeView(1);
 68        }

 69
 70        private void radType_CheckedChanged(object sender, EventArgs e)
 71        {
 72            InitTreeView(1);
 73        }

 74
 75        private void radSupplier_CheckedChanged(object sender, EventArgs e)
 76        {
 77            InitTreeView(2);
 78        }

 79
 80        private void radCity_CheckedChanged(object sender, EventArgs e)
 81        {
 82            InitTreeView(3);
 83        }

 84
 85        private void butSelectFile_Click(object sender, EventArgs e)
 86        {
 87            string curWD = System.IO.Directory.GetCurrentDirectory();
 88            OpenFileDialog fd = new OpenFileDialog();
 89            if (fd.ShowDialog(this== DialogResult.OK)
 90            {            
 91                localFileName = fd.SafeFileName;
 92                FileInfo fi = new FileInfo(fd.SafeFileName);
 93                String path = fi.Directory.ToString();
 94                this.localFilePath = path + "\\" + localFileName;
 95                textFile.Text = localFilePath;
 96                
 97            }

 98            System.IO.Directory.SetCurrentDirectory(curWD);
 99
100        }

101
102        private void Netree_MouseClick(object sender, MouseEventArgs e)
103        {
104            if(e.Button == MouseButtons.Left)
105            {
106                TreeNode tn = this.Netree.GetNodeAt(e.Location);
107
108                if (tn != null && tn.Bounds.Contains(e.Location))
109                {
110                    if (tn.Nodes.Count == 0)
111                    {
112                        curTN = tn;
113                        取网元名
116
117                        根据网元名找设备类型和厂商
120
121                        读配置文件,把文件类型放入文件类型链表
125
126                        加载文件类型链表
135
136                    }

137                }

138            }

139        }

140        private void GetTypeSuppler(String nn, out String type, out String suppler)
141        {
142
143            foreach (NeInfoLocal ni in m_NeList)
144            {
145                if (ni.strNeNameEN.Equals(nn))
146                {
147                    type = ni.strNeType;
148                    suppler = ni.strNeSupplier;
149                    return;
150                }

151            }

152            type = null;
153            suppler = null;
154        }

155        private String[] GetFileType(string type, string suppler)
156        {
157            String[] fileTypeList;
158            String res = null;
159            XmlDocument xmlDoc = new XmlDocument();
160            xmlDoc.Load("FileTyp.xml");
161             XmlNode node = xmlDoc.SelectSingleNode("FileType");
162             for (int i = 0; i < node.ChildNodes.Count; i++)
163             {
164                 if (node.ChildNodes[i].Name.Equals(type))
165                 {
166                     for (int j = 0; j < node.ChildNodes[i].ChildNodes.Count; j++)
167                     {
168                         if (node.ChildNodes[i].ChildNodes[j].Name.Equals(suppler))
169                         {
170                             res = node.ChildNodes[i].ChildNodes[j].InnerText.ToString();
171                             fileTypeList = res.Split(',');
172                             return fileTypeList;
173                         }

174                     }

175                 }

176             }

177             return null;
178        }

179        private void SetFtpInfo()
180        {
181            XmlDocument xmlDoc = new XmlDocument();
182            xmlDoc.Load("UpLoadFtp.xml");
183            XmlNode node = xmlDoc.SelectSingleNode("UpLoad");
184            for (int i = 0; i < node.ChildNodes.Count; i++)
185            {
186                if (node.ChildNodes[i].Name.Equals("IP"))
187                {
188                    ip = node.ChildNodes[i].InnerText;
189                }

190                else if (node.ChildNodes[i].Name.Equals("userName"))
191                {
192                    username = node.ChildNodes[i].InnerText;
193                }

194                else if (node.ChildNodes[i].Name.Equals("password"))
195                {
196                    pwd = node.ChildNodes[i].InnerText;
197                }

198                else if (node.ChildNodes[i].Name.Equals("remotePath"))
199                {
200                    remotepath = node.ChildNodes[i].InnerText;
201                }

202            }

203        }

204        private void button1_Click(object sender, EventArgs e)
205        {
206            lbFtpMessage.Text = "";
207            //初始化ftp信息
208            SetFtpInfo();
209            uploadFtp = new UpLoadFile(ip, username, pwd);
210            isRename = this.checkIsFormat.Checked;
211            if (this.textFile.Text.Length == 0)
212            {
213                MessageBox.Show("请选择要上传的文件""信息提示");
214            }

215            else
216            {
217                if (isRename == true)
218                {
219                    if (curTN == null)
220                    {
221                        MessageBox.Show("请选择网元""提示信息");
222                        return;
223                    }

224                    if(comboFileType.SelectedItem == null)
225                    {
226                        MessageBox.Show("请选择文件类型","信息提示");
227                        return;
228                    }

229                }

230               
231                //上传
232                FileInfo fi = new FileInfo(localFilePath);
233                this.localFileLength = fi.Length;
234                uploadFtp.BytesTransferred += new EnterpriseDT.Net.Ftp.BytesTransferredHandler(uploadFtp_BytesTransferred);
235                uploadFtp.TransferComplete += new EnterpriseDT.Net.Ftp.TransferHandler(uploadFtp_TransferComplete);
236                if (this.checkIsFormat.Checked)
237                {
238                    Random ran = new Random();
239                    remoteFileName = curtype + "#" + cursuppler + "#" + curnetName + "#"
240                                   + comboFileType.SelectedItem.ToString()
241                                   + "#" + DateTime.Now + "#" + ran + "#"
242                                   + "0.src";
243                }

244                else
245                {
246                    remoteFileName = localFileName;
247                }

248                uploadFtp.MyUpload(remotepath, localFilePath, remoteFileName);
249               
250                
251            }

252        }

253
254        void uploadFtp_TransferComplete(object sender, EnterpriseDT.Net.Ftp.TransferEventArgs e)
255        {
256            if (this.checkIsFormat.Checked)
257            {
258                Random ran = new Random();
259                this.fileNewName = curtype + "#" + cursuppler + "#" + curnetName + "#"
260                                   + comboFileType.SelectedItem.ToString()
261                                   + "#" + DateTime.Now + "#" + ran + "#"
262                                   + "1.src";
263                uploadFtp.ReName(remoteFileName, fileNewName);
264            }

265            progressBar1.Value = 0;
266            lbFtpMessage.ForeColor = Color.Red;
267            lbFtpMessage.Text = "上传成功!";
268        }

269
270        void uploadFtp_BytesTransferred(object sender, EnterpriseDT.Net.Ftp.BytesTransferredEventArgs e)
271        {
272            this.progressBar1.Value = (int)(100 *((float)e.ByteCount / (float)localFileLength));
273
274            string str = String.Format("上传进度: {0}%", progressBar1.Value);
275            lbFtpMessage.ForeColor = Color.Black;
276            lbFtpMessage.Text = str;
277            Application.DoEvents();
278        }

279
280        private void comboFileType_TextChanged(object sender, EventArgs e)
281        {
282
283            获取文件类型
288        }

289
290        private void butCancle_Click(object sender, EventArgs e)
291        {
292
293            this.Close();
294        }

295
296    }

297}
posted on 2008-10-31 10:04 天书 阅读(3482) 评论(1)  编辑 收藏 引用

FeedBack:
# re: ftp上传下载进度条ffff
2013-03-27 17:57 | ca
123  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理



<2010年11月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜