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.

1 comment:

XeNoN said...

Are you sure you need those lines:

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

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