2013-04-20 55 views
1

我想弄清楚這個元素,我做了一個ftp模塊,除了上傳方法外,其他幾個操作都完美無缺。事實上它確實有效,但它仍然會返回一個錯誤。FTP上傳給出超時,但上傳文件

所以我在一段時間後出現超時錯誤,但是當我檢查我的ftp時,文件被成功添加。所以,我不明白這個功能..

我的代碼:

public static Boolean upload(string remoteFile, string localFile, string applicatie_url) { 
     Boolean returnbool = false; 

     try { 

      FtpWebRequest ftpRequest; 
      FtpWebResponse ftpResponse = null; 
      Stream ftpStream = null; 

      ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile); 
      ftpRequest.Credentials = new NetworkCredential(user, pass); 
      ftpRequest.UseBinary = true; 
      ftpRequest.UsePassive = true; 
      ftpRequest.KeepAlive = true; 
      ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; // upload 

      byte[] b = File.ReadAllBytes(localFile); 
      ftpRequest.ContentLength = b.Length; 
      ftpStream = ftpRequest.GetRequestStream(); 

      try { 

       ftpStream.Write(b, 0, b.Length); 
       ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); 
      } catch (Exception ex) { 
            // Error catching 
      } 

      // Cleanup 
      ftpStream.Close(); 
      ftpRequest = null; 
      ftpResponse = null; 
      returnbool = true; 

     } catch (Exception ex) { 
      // Error catching 
     } 

     return returnbool; 
    } 

我ex.message: '傳輸超時。'

我ex.stacktrace: '' 在System.Net.FtpWebRequest.EndGetResponse(IAsyncResult的asyncResult)[0x00052]在/用戶/助洗劑/數據/通道/單MAC-UI-刷新-2-10/2baeee2f /source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10.11.git/mcs/class/System/System.Net/FtpWebRequest.cs:411 at System.Net.FtpWebRequest.GetResponse()[0x00009] in/Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build- root/mono-2.10.11/_build/mono-2.10.11.git/mcs/class/System/System.Net/FtpWebRequest.cs:426 at AutoPolis.FTPHelper.upload(System.String remoteFile,System.String localFile ,System.String applicatie_url)[0x00078] in /Users/.../App_Code/FTPHelper.cs:92'

任何幫助,將不勝感激。我使用MonoDevelop作爲IDE工作,不知道這是否使這個元素有任何機會..

+1

是文件上傳完成? – Filip 2013-04-20 12:24:27

+0

當我檢查我的ftp時,我確實可以訪問該文件(本例中爲鏡像)。我可以看到它沒有任何問題,所以沒有損壞的文件或類似的東西。試用這個文件http://yosoyke.be/Veloster/Hyundai.jpg – Kevinc 2013-04-20 12:29:47

回答

1

我已經看過幾個ftp程序,並注意到在你的代碼清理,你已經寫了ftpResponse = null;但我會去「ftpResponse.Close();」。你的堆棧跟蹤似乎提到FTPWebRequest,所以這可能會有所幫助!

p.s.這可能有所幫助:http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class

+1

好吧,thx,並沒有真正關閉響應,但確實發生了確切的錯誤。我注意到在我的代碼中,我不需要ftpresponse,所以我可以完全刪除該部分。 (代碼的確是基於給定的url !!) – Kevinc 2013-04-20 15:22:12