現在做的一個項目是用的silverlight做的。 但是遇到一個問題 就是FTP下載的時候 獲取GetResponse時 是超時的響應。 在百度上搜了很多的資料都沒有解決呢。 結果發現解決其實很簡單。 見下測紅色代碼。
//FTP下載操作 public bool getFile(string FTP, string Account, string PassWord,string FileName,string LocalPath) { FtpWebRequest reqFTP; try { FileStream outputStream = new FileStream(LocalPath, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTP + "/" + FileName)); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.UsePassive = true; reqFTP.KeepAlive = true; reqFTP.Credentials = new NetworkCredential(Account, Password); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); return true; } catch (Exception err) { ExceptionRecord("FTP下載", err.Message + "/r/n" + err.StackTrace); return false; } }
這個樣子的FTP函數。 就能在c#中使用。 同時也能被silverlight的wcf層進行引用了~
新聞熱點
疑難解答