Thanks to the .NET Framework 2.0 , all the basic functionality to access an FTP server is already built into C #.
This method downloads file from FTP and add date time to new File name.
Key points
- Each application will require an object attached to its NetworkCredentials Credentials property. This tells the application how to authenticate against the FTP server.
- The URI provided in order of application must include the name of the file you need to load or unload. For example, if we are downloading the file "data.xml" of abc.ftp.com our ftp://abc.ftp.com/data.xml URI.
- You need to be familiar with the Stream object. We will use this object, both upload and download files.
- These can be simple, when I started moving files to / from FTP servers do not understand some of these concepts, so I thought it would be important! Now go to the code.
Steps to follow
1. Create ftp object
2. Login to ftp server using userid and password
3. Set keepalive property true to multiple use of same instance
4. Use binary method to upload data
5. Set action to check the date and time of ftp file
5. Call our used defined method to download file
6. Open file in filesteam
7. Read the FTP file in buffer
8. Save file at local drive
- public void DownloadFile(string ftppath, string destfile,string strFileName)
- {
- try
- {
- //create ftp object
- FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftppath);
- //userid and password for the ftp server
- ftp.Credentials = new NetworkCredential("ftp_username", "ftp_password");
- //Set keepalive property true to multiple use of same instance
- ftp.KeepAlive = true;
- //use binary method to upload data
- ftp.UseBinary = true;
- //set action to check the date and time of ftp file
- ftp.Method = WebRequestMethods.Ftp.GetDateTimestamp;
- FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
- //recieve ftp file date time
- DateTime modification = response.LastModified;
- // Download the data
- string sBackupFileName = strFileName + modification.ToString("MM_dd_yyyy") + "_" + modification.Hour.ToString() + "_" + modification.Minute.ToString() + "_" + modification.Second.ToString() + ".xml";
- //download file in byte , for this call another user defined method given in this post
- byte[] Data = DownloadData(ftppath);
- // Save the data to disk
- FileStream fs = new FileStream(destfile + sBackupFileName, FileMode.Create);
- fs.Write(Data, 0, Data.Length);
- //close the file stream
- fs.Close();
- }
- catch (Exception ex)
- {
- //exception handling
- throw;
- }
- }
- //user define method to download data from FTP
- public byte[] DownloadData(string path)
- {
- // Get the object used to communicate with the server.
- WebClient request = new WebClient();
- // Logon to the server using username + password
- ftp.Credentials = new NetworkCredential("ftp_username", "ftp_password");
- return request.DownloadData(path);
- }
How to call method:
DownloadFile("ftp://abc.ftp.com/data.xml", Server.MapPath("/abc/Documents/"), "new_file");
0 comments:
Post a Comment