Getting Images from FTP using C#

Hi all,

Here the sample for getting an Image from ftp file path using C#.

using System.Net;

string path = "ftp://dev-ftp.test.com/" + folderName + "\\" + fileName;

FtpWebRequest ftp;

ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));

ftp.Credentials = new NetworkCredential(username, password);

ftp.Method = WebRequestMethods.Ftp.DownloadFile;

using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())

{

using (Stream responseStream = response.GetResponseStream())

{

using (FileStream fs = new FileStream(localPath+fileName, FileMode.Create))

{

byte[] buffer = new byte[102400];

int read = 0;

do

{

read = responseStream.Read(buffer, 0, buffer.Length);

fs.Write(buffer, 0, read);

fs.Flush();

} while (!(read == 0));

fs.Flush();

fs.Close();

response.Close();

responseStream.Close();


Hence we can save an image in to our local file path from ftp file path.

That's it....

...S.VinothkumaR.

2 comments:

XeNoN said...

Are you sure you need those lines:

response.Close();
responseStream.Close();

when using the "(using ...){ ... }" syntax ?

Asava Samuel said...

This is a great FTP Library for C#, it is reasonably priced too:
https://www.kellermansoftware.com/p-39-net-ftp-library.aspx