This is a discussion on How to Download and Upload files in FTP? within the C# Programming forums, part of the Software Development category; How to Download and Upload files in FTP? Include the below namespaces to work with the FTP download. using System....
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| How to Download and Upload files in FTP? Include the below namespaces to work with the FTP download. using System.Net; using System.IO; FTP Download File: public bool DownloadFile(string sourceFilePath,string ftpUserName,string ftpPassword) { string fileName = sourceFilePath.Substring((sourceFilePath.LastIndex Of('/') + 1)); FtpWebRequest ftp; ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(sourceFilePath)); ftp.Credentials = new NetworkCredential(ftpUserName,ftpPassword); ftp.Method = WebRequestMethods.Ftp.DownloadFile; ftp.Timeout = (int)new TimeSpan(0, 5, 0).TotalMilliseconds; ftp.ReadWriteTimeout = 999999; ftp.ServicePoint.MaxIdleTime = 999999; using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { //loop to read & write to file using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("Dow nloadFiles") + "\\" + fileName, FileMode.Create)) { try { byte[] buffer = new byte[1024]; int read = 0; long count = 0; do { read = responseStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, read); count++; } while (!(read == 0)); responseStream.Close(); fs.Flush(); fs.Close(); return true; } catch (Exception) { fs.Close(); //delete target file as it's incomplete //targetFI.Delete(); responseStream.Close(); response.Close(); return false; } } } } } FTP Upload File: public bool UploadFile(string sourceFilePath,string ftpUserName,string ftpPassword) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(sourceFilePath); request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential (ftpUserName, ftpPassword); // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader("testfile.txt"); byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //Upload status completed if response is not null response.Close(); } |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Upload Files to MySQL using PHP Tips & Tricks | Sabari | PHP Programming | 3 | 12-18-2007 06:32 AM |
| gOS download...finally! | kmcgra | Technology BUZZzzzzz | 0 | 11-17-2007 09:22 AM |
| Trace files and Log Files in Unix | vigneshgets | Operating Systems | 0 | 08-01-2007 05:18 AM |
| Download a file using Perl.. | raj | Perl | 0 | 07-18-2007 03:45 AM |
| File Download From a URL | moinuddin102 | PHP Programming | 1 | 05-01-2007 06:51 AM |