2014-02-13 133 views
0

我有使用FTP,即時通訊上傳在C# 的方式即時通訊做它不工作 我得到一個錯誤,說流關閉3演出XML文件.. 。
和0字節的文件,當我做一個小flie它的工作原理... 我需要幫助:)如何上傳使用FTP 3gig XML文件在C#中

m_reset.Reset(); 
FtpClient conn = new FtpClient(); 
_LocalPath = LocalFilePath; 
conn.Host = record.Attribute("ftpServer").Value.ToString(); 
conn.Credentials = new NetworkCredential(record.Attribute("ftpUser").Value.ToString(), record.Attribute("ftpPass").Value.ToString()); 
string fname = DateTime.Now.ToString("EnvisionPush-{0:yyyy-MM-dd_hh_mm_ss_tt}"+".xml"); 
conn.BeginOpenWrite(fname, new AsyncCallback(BeginOpenWriteCallback), conn); 
m_reset.WaitOne(); 
conn.Disconnect(); 


static void BeginOpenWriteCallback(IAsyncResult ar) { 
    FtpClient conn = ar.AsyncState as FtpClient; 
    Stream istream = null, ostream = null; 
    byte[] buf = new byte[8192]; 
    int read = 0; 

    try { 
     if (conn == null) 
      throw new InvalidOperationException("The FtpControlConnection object is null!"); 

     ostream = conn.EndOpenWrite(ar); 
     istream = new FileStream(_LocalPath, FileMode.Open, FileAccess.Read); 

     while ((read = istream.Read(buf, 0, buf.Length)) > 0) { 
      ostream.Write(buf, 0, read); 
     } 
    } 
    catch (Exception ex) { 
     Console.WriteLine(ex.ToString()); 
    } 
    finally { 
     try { 
      if (istream != null) 
       istream.Close(); 

      if (ostream != null) 
       ostream.Close(); 

      m_reset.Set(); 

      } 
      catch (Exception e) { 
      } 
     } 
    } 
+0

Hav you any error? –

+0

是的錯誤是一個流關閉錯誤 – Rico

回答

0

使用此sample

/// <summary> 
/// Methods to upload file to FTP Server 
/// </summary> 
/// <param name="_FileName">local source file name</param> 
/// <param name="_UploadPath">Upload FTP path including Host name</param> 
/// <param name="_FTPUser">FTP login username</param> 
/// <param name="_FTPPass">FTP login password</param> 
public void UploadFile(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass) 
{ 
    System.IO.FileInfo _FileInfo = new System.IO.FileInfo(_FileName); 

    // Create FtpWebRequest object from the Uri provided 
    System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath)); 

    // Provide the WebPermission Credintials 
    _FtpWebRequest.Credentials = new System.Net.NetworkCredential(_FTPUser, _FTPPass); 

    // By default KeepAlive is true, where the control connection is not closed 
    // after a command is executed. 
    _FtpWebRequest.KeepAlive = false; 

    // set timeout for 20 seconds 
    _FtpWebRequest.Timeout = 20000; 

    // Specify the command to be executed. 
    _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile; 

    // Specify the data transfer type. 
    _FtpWebRequest.UseBinary = true; 

    // Notify the server about the size of the uploaded file 
    _FtpWebRequest.ContentLength = _FileInfo.Length; 

    // The buffer size is set to 2kb 
    int buffLength = 2048; 
    byte[] buff = new byte[buffLength]; 

    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded 
    System.IO.FileStream _FileStream = _FileInfo.OpenRead(); 

    try 
    { 
     // Stream to which the file to be upload is written 
     System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream(); 

     // Read from the file stream 2kb at a time 
     int contentLen = _FileStream.Read(buff, 0, buffLength); 

     // Till Stream content ends 
     while (contentLen != 0) 
     { 
      // Write Content from the file stream to the FTP Upload Stream 
      _Stream.Write(buff, 0, contentLen); 
      contentLen = _FileStream.Read(buff, 0, buffLength); 
     } 

     // Close the file stream and the Request Stream 
     _Stream.Close(); 
     _Stream.Dispose(); 
     _FileStream.Close(); 
     _FileStream.Dispose(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
} 
+0

這將工作3演出嗎? – Rico

+0

請嘗試;)。 –

+0

您是否嘗試過使用標準ftp客戶端上傳3gig文件?服務器可能不接受這個大小的文件? – bdn02