IT Community - Software Programming, Web Development and Technical Support

How to Download and Upload files in FTP?

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....


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-16-2007, 11:31 PM
Archer Archer is offline
D-Web Programmer
 
Join Date: Jun 2007
Posts: 52
Archer is on a distinguished road
Default How to Download and Upload files in FTP?

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();
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 05:34 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0