2014-01-09 61 views
0

對於Cron服務,我在本地機器上的.log文件中記錄錯誤,然後我將它上傳到ftp。所以我的問題是在ftp上傳文件後,我得到了不完整的日誌文件格式不正確,在本地機器上它是正確的。任何人都可以幫助我,讓我知道我失蹤的地方。從本地機器上傳.log文件到FTP

實例登錄本地機器上:

時間戳:2014年1月8日下午5點五十分14秒

候選人電子郵件:[email protected]

狀態:成功:創建

相同的日誌上的ftp:

TE電子郵件:[email protected]

狀態:成功:創建

我想這個代碼:

using (FileStream fileStream = new FileInfo(Convert.ToString(ConfigurationManager.AppSettings["DestinationLocation"]) + @"\" + fileName).Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
      { 
       int bufferLength = 2048; 
       byte[] buffer = new byte[bufferLength]; 

       using (Stream uploadStream = request.GetRequestStream()) 
       { 
        int contentLength = fileStream.Read(buffer, 0, bufferLength); 

        while (contentLength != 0) 
        { 
         uploadStream.Write(buffer, 0, bufferLength); 
         contentLength = fileStream.Read(buffer, 0, bufferLength); 
        } 
        fileStream.Close(); 
        uploadStream.Close(); 
       } 
      } 
+1

是不同的文件大小? –

回答

0

請試試這個。您的代碼不考慮讀取信息的大小,並且看起來總是保存以免被讀取。

using (FileStream fileStream = new FileInfo(Convert.ToString(ConfigurationManager.AppSettings["DestinationLocation"]) + @"\" + fileName).Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
    { 
     byte[] buffer = new byte[fileStream.Length]; 
     int numBytesToRead = (int)fileStream.Length; 
     int numBytesRead = 0; 

     if (numBytesToRead > 0) 
     {  
      // get all file information in to the buffer 
      while (numBytesToRead > 0) 
      { 
       int n = fileStream.Read(buffer, numBytesRead, numBytesToRead); 

       if (n == 0) 
        break; 

       numBytesRead += n; 
       numBytesToRead -= n; 
      } 

      // now write that data to the file  
      using (Stream uploadStream = request.GetRequestStream()) 
      { 
       uploadStream.Write(buffer, 0, buffer.Length); 
      } 
     } 
    }