| Author: Shanthi M 06 Sep 2008 | Member Level: Gold | Rating: Points: 3 |
Secure FTP Factory for .NET provides easy to use classes for communicating with an FTP server using the FTP or secure FTPS protocols.
Creating a new Ftp instance
Before creating a new Ftp instance, ensure that the Jscape.Ftp namespace is defined in your using statements, and that the Jscape.Ftp.dll is referenced in your project. Refer to the Getting Started topic in the Secure FTP Factory for .NET User Guide for more information about adding the Jscape.Ftp.dll reference to your projects.
Create a new Ftp instance providing the server hostname, username, and password as arguments.
Ftp myFtp = new Ftp("hostname", "username", "password");
The Ftp class allows for client connections to an FTP server using FTP, AUTH TLS, or Implicit SSL protocols.
FTP Client connects to server on port 21 using standard FTP.
AUTH TLS Client connects to server on port 21 (the default FTP port) and issues a series of commands to establish a secure connection.
Implicit SSL Client connects to server on port 990 and establishes a secure tunnel using SSL. No additional FTP commands are required to establish a connection.
Consult your FTP server documentation to see which mode(s) your FTP server supports. Not all FTP servers support secure FTP connections using SSL. Of those that do support SSL some may only support one of the methods AUTH TLS or Implicit SSL.
To establish a secure connection to an FTP server, set the ConnectionType property. For secure connections you may connect using AUTH TLS or Implicit SSL. The default connection type is to connect using standard FTP. The example below demonstrates configuring the Ftp instance to connect using AUTH TLS.
Ftp myFtp = new Ftp("hostname", "username", "password"); myFtp.ConnectionType = Ftp.AUTH_TLS; // or use Ftp.IMPLICIT_SSL
Subscribing to FTP Events
You may capture FTP related events using the event classes found in Secure FTP Factory for .NET. To capture FTP events subscribe your Ftp instance to the events you wish to capture prior to invoking the Connect() method. This ensures that all data sent to and from the FTP server is captured by your event handling code.
// Subscribe to events myFtp.FtpConnectedEvent += new FtpConnectEventHandler(OnConnected); myFtp.FtpDisconnectedEvent += new FtpConnectEventHandler(OnDisconnected); myFtp.FtpCommandEvent += new FtpCommandEventHandler(OnCommand); myFtp.FtpResponseEvent +=new FtpCommandEventHandler(OnResponse); myFtp.FtpDownloadEvent +=new FtpDownloadEventHandler(OnDownload); myFtp.FtpUploadEvent +=new FtpDownloadEventHandler(OnUpload); myFtp.FtpProgressEvent +=new FtpProgressEventHandler(OnProgress); myFtp.FtpListingEvent +=new FtpListingEventHandler(OnListing); myFtp.FtpConnectionLostEvent +=new FtpConnectEventHandler(OnConnectionLost);
Now you can create the event handler methods you previously defined when subscribing to the FTP events.
private void OnConnected(object sender, FtpConnectEventArgs e) { Console.WriteLine("Ftp connected to " + e.HostName); }
private void OnDisconnected(object sender, FtpConnectEventArgs e) { Console.WriteLine("Disconnected " + e.HostName); }
Establishing a connection
Once an Ftp instance has been created you may establish a connection to the FTP server by invoking the Connect() method.
myFtp.Connect();
Perform a remote directory listing
The Ftp class contains methods to retrieve a remote directory listing. It shows how to retrieve and display a directory listing as a string.
string dirList = myFtp.GetDirListingAsString(); Console.WriteLine("Remote Dir Listing\r\n" + dirList);
Retrieving remote file data
You can retrieve specific property information about the files in the remote directory using the FtpFile instance. It shows how to retrieve and display some of the properties of the files in a remote directory listing.
IEnumerator e = myFtp.GetDirListing(); while (e.MoveNext()) { FtpFile file = (FtpFile)e.Current; Console.WriteLine("Filename: " + file.Filename); if (file.IsDirectory) { Console.WriteLine("\tDir Owner/Group: "+file.Owner + "/"+file.Group); }else if(file.Link) { Console.WriteLine("\tLink Target: "+file.LinkTarget); }else{ Console.WriteLine("\tFile Size: "+file.Size); Console.WriteLine("\tPermissions: "+file.Permission); Console.WriteLine("\tModify Date: "+file.Date); Console.WriteLine("\tModify Time: "+file.Time); } Console.WriteLine(); }
Downloading a file
You can download one or more files from the current remote directory to your local directory. If you know the file name you want to download, use the Download() method. If you want to download files based on a file filter, use the MDownload() method.
It uses the MDownload() method to download all files from the remote directory with a .log file extension. You may set the remote directory using the RemoteDir property.
// set remote dir to logs dir myFtp.RemoteDir = "logs";
// download all log files myFtp.MDownload("*.log");
Uploading a file
As with downloading files, the Ftp class provides methods to upload one or more files from the current local directory to the current remote directory. If you know the file name you want to upload, use the Upload() method. If you want to upload files based on a file filter, use the MUpload() method.
It uses the MUpload() method to upload all files from the local directory with a .jpg file extension to the images directory. You may set the remote directory using the RemoteDir property.
// set remote dir to site's images dir myFtp.RemoteDir = "yoursite.com/images";
// upload all jpg files myFtp.MUpload("*.jpg");
Releasing a connection
To release a connection simply invoke the Disconnect() method as follows:
myFtp.Disconnect();
|
| Author: Gitolekha Ray 07 Sep 2008 | Member Level: Silver | Rating: Points: 3 |
Hi Jigar, See if this helps, FtpWebRequest reqFTP; Response.Buffer = true; Response.Clear(); Response.AddHeader(”Content-Disposition”, “inline; filename=” + filename); Response.ContentType = “application/x-zip-compressed”; Response.Flush(); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(”ftp://” + ftpServerIP + “/” + filename)); reqFTP.Proxy = null; reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword, ftpDomain); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); MemoryStream MemStream = new MemoryStream(); int bufferSize = 1024; byte[] respBuffer = new byte[bufferSize]; int bytesRead = ftpStream.Read(respBuffer, 0, respBuffer.Length); while (bytesRead > 0) { MemStream.Write(respBuffer, 0, bytesRead); bytesRead = ftpStream.Read(respBuffer, 0, respBuffer.Length); } Response.BinaryWrite(MemStream.ToArray());
|
| Author: Jigar Joshi 09 Sep 2008 | Member Level: Gold | Rating: Points: 3 |
Hey Shanthi,
Thanks for your reply.
This obvious seems to be utilizing third party tool called "JScape".
Is there any (free) tool available for handling this? Can't native asp.net codes (v 2.0 or 3.5) handle this?
My client won't like in purchasing this tool atleast for this small ftp operation!
Regards Jigar
|