Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > Unable to read data from the transport
- > connection: A connection attempt
- > failed because the connected party did
- > not properly respond after a period of
- > time, or established connection failed
- > because connected host has failed to
- > respond.
- ...
- private void CreateDownloadFile()
- {
- _OutputFile = new FileStream(_SourceFile, FileMode.Create);
- }
- public string FTPDownloadFile()
- {
- this.CreateDownloadFile();
- myReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(this.DownloadURI));
- myReq.Method = WebRequestMethods.Ftp.DownloadFile;
- myReq.UseBinary = true;
- myReq.Credentials = new NetworkCredential(_ID, _Password);
- FtpWebResponse myResp = (FtpWebResponse)myReq.GetResponse();
- Stream ftpStream = myResp.GetResponseStream();
- int bufferSize = 2048;
- int readCount;
- byte[] buffer = new byte[bufferSize];
- int bytesRead = 0;
- readCount = ftpStream.Read(buffer, 0, bufferSize);
- while (readCount > 0)
- {
- _OutputFile.Write( buffer, 0, readCount );
- readCount = ftpStream.Read( buffer, 0, bufferSize );
- Console.Write( '.' ); // show progress on the console
- bytesRead += readCount;
- }
- Console.WriteLine();
- logger.logActivity( " FTP received " + String.Format( "{0:0,0}", bytesRead ) + " bytes" );
- ftpStream.Close();
- _OutputFile.Close();
- myResp.Close();
- return this.GetFTPStatus();
- }
- public string GetFTPStatus()
- {
- return ((FtpWebResponse)myReq.GetResponse()).StatusDescription;
- }
- FtpWebRequest reqFTP;
- string fileName = @"c:downloadDirlocalFileName.txt";
- FileInfo downloadFile = new FileInfo(fileName);
- string uri = "ftp://ftp.myftpsite.com/ftpDir/remoteFileName.txt";
- FileStream outputStream = new FileStream(fileName, FileMode.Append);
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
- reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
- reqFTP.UseBinary = true;
- reqFTP.KeepAlive = false;
- reqFTP.Timeout = -1;
- reqFTP.UsePassive = true;
- reqFTP.Credentials = new NetworkCredential("userName", "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);
- Console.WriteLine("Connected: Downloading File");
- while (readCount > 0)
- {
- outputStream.Write(buffer, 0, readCount);
- readCount = ftpStream.Read(buffer, 0, bufferSize);
- Console.WriteLine(readCount.ToString());
- }
- ftpStream.Close();
- outputStream.Close();
- response.Close();
- Console.WriteLine("Downloading Complete");
Add Comment
Please, Sign In to add comment