Friday, January 30, 2015

Programmatically Upload and Download Files using FTP and C#

FTP is still the standard for online downloads and uploads, but to utilize it in .NET you can just use the System.Net.FtpWebRequest class which exposes all the functionality of FTP without having to know how FTP works.
Thus, C# can perform FTP uploads and downloads using the System.Net.FtpWebRequest class.

FTP Downloading using.NET

FtpWebRequest objects are created using the WebRequest class’ Create method and specifying the URI for the FTP download. In the below example the file sample.zip is the target for the download. A FileStream is first opened for the target and is then wrapped by a BinaryWriter. The BinaryReader object is created with the response stream from the FtpWebRequest object. The stream is then read and the target is written until the file has been fully downloaded.
 FtpWebRequest requestObj = (FtpWebRequest)WebRequest.Create(
    "ftp://ftp.yourdomain.com/dir/subdir/sample.zip");
    requestObj.Credentials = new NetworkCredential("anonymous", "hilyard@oreilly.com");
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse( ))

FTP Uploading using .NET

To upload a file using C# you should use an FtpWebRequest object to get a stream on the request using GetRequestStream and then use it to upload the file. When the file has been opened and then written to the request stream, execute the request by calling GetResponse and then check the StatusDescription property for the operations’s result:
string uploadFileStr = "SampleClassLibrary.dll";
Uri ftpSite =
new Uri("ftp://localhost/Upload/" + uploadFileStr);
FileInfo fileInfo = new FileInfo(uploadFileStr);
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(
ftpSite);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.ContentLength = fileInfo.Length;
request.Credentials = new NetworkCredential("user", "noaccount@domain.com");
byte[] byteBuffer = new byte[4096];
using (Stream requestStream = request.GetRequestStream( ))
{
using (FileStream fileStreamObj = new FileStream(uploadFileStr, FileMode.Open))
{
int bytesRead = 0;
do
{
bytesRead = fileStreamObj.Read(byteBuffer, 0, byteBuffer.Length);
if (bytesRead > 0)
{
requestStream.Write(byteBuffer, 0, bytesRead);
}
{
Stream dataObj = response.GetResponseStream( );
string targetPathStr = "sample.zip";
if (File.Exists(targetPathStr))
File.Delete(targetPathStr);
byte[] byteBuffer = new byte[4096];
using (FileStream output = new FileStream(targetPathStr, FileMode.CreateNew))
{
int bytesRead = 0;
do
{
bytesRead = dataObj.Read(byteBuffer, 0, byteBuffer.Length);
if (bytesRead > 0)
{
output.Write(byteBuffer, 0, bytesRead);
}
}
while (bytesRead > 0);
}
}
    } while (bytesRead > 0); } } using (FtpWebResponse response =
    (FtpWebResponse)request.GetResponse( )) {
    Console.WriteLine(response.StatusDescription); }
That’s it! Now you can write .NET apps which incorporate FTP upload/download using C#.

No comments:

Post a Comment