FTP上传

 if (File.Exists(fileName))
                {
                    FileInfo fileInf 
= new FileInfo(fileName);
                    
string uri = FtpUrl + "/" + fileInf.Name;
                    
//FTP
                    FtpLink = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                    FtpLink.UseBinary 
= true;
                    FtpLink.Credentials 
= new NetworkCredential(FtpUserName, FtpUserPwd);
                    FtpLink.KeepAlive 
= false;
                    FtpLink.Method 
= WebRequestMethods.Ftp.UploadFile;
                    FtpLink.ContentLength 
= fileInf.Length;

                    
int BuffLength = 2048;
                    
byte[] buff = new byte[BuffLength];
                    
int contentLen;
                    FileStream fs 
= fileInf.OpenRead();
                    
try
                    {
                        Stream strm 
= FtpLink.GetRequestStream();
                        logger.Trace(DateTime.Now 
+ ",Ftp:文件总大小:" + fileInf.Length / 1024 + "k");
                        contentLen 
= fs.Read(buff, 0, BuffLength);
                        
int OKlen = 0;
                        
while (contentLen != 0)
                        {

                            strm.Write(buff, 
0, contentLen);
                            OKlen 
+= contentLen;

                            
//Logger.WriteInfo(FtpName, DateTime.Now.ToString() + ",Ftp:文件总大小:" + fileInf.Length / 1024 + "k,已经上传: " + OKlen / 1024 + "k");
                            contentLen = fs.Read(buff, 0, BuffLength);
                        }
                        strm.Close();
                        fs.Close();
                        logger.Trace(DateTime.Now 
+ ":上传完毕");
                        File.Delete(fileName);

                    }
                    
catch (Exception ex)
                    {
                        logger.Error(DateTime.Now 
+ ":" + ex.Message);
                    }
                }
posted @ 2011-06-08 15:19  小白.net  阅读(386)  评论(0编辑  收藏  举报