0% found this document useful (0 votes)
80 views2 pages

FTP Transfer

This document provides code examples for uploading and downloading files to/from an FTP server in C#. It includes functions to upload a file by specifying the FTP URL, file name, username, password, and optional subdirectory. It also includes a function to download a file by specifying the FTP URL, file name, username, password, and local path to save the downloaded file. Both functions return a status string from the server response.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views2 pages

FTP Transfer

This document provides code examples for uploading and downloading files to/from an FTP server in C#. It includes functions to upload a file by specifying the FTP URL, file name, username, password, and optional subdirectory. It also includes a function to download a file by specifying the FTP URL, file name, username, password, and local path to save the downloaded file. Both functions return a status string from the server response.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Upload and Download file through FTP in C#

using System.Net;
using System.IO;

// Upload File to Specified FTP Url with username and password and Upload
Directory
//if need to upload in sub folders ///
//Base FtpUrl of FTP Server
//Local Filename to Upload
//Username of FTP Server
//Password of FTP Server
//[Optional]Specify sub Folder if any
//Status String from Server
public static string UploadFile(string FtpUrl, string fileName, string
userName, string password,string
UploadDirectory="")
{
string PureFileName = new FileInfo(fileName).Name;
String uploadUrl = String.Format("{0}{1}/{2}",
FtpUrl,UploadDirectory,PureFileName);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
req.Proxy = null;
req.Method = WebRequestMethods.Ftp.UploadFile;
req.Credentials = new NetworkCredential(userName,password);
req.UseBinary = true;
req.UsePassive = true;
byte[] data = File.ReadAllBytes(fileName);
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
FtpWebResponse res = (FtpWebResponse)req.GetResponse();
return res.StatusDescription;
}

//Download File From FTP Server


//Base url of FTP Server
//if file is in root then write FileName Only if is in use like
"subdir1/subdir2/filename.ext"
//Username of FTP Server
//Password of FTP Server
//Folderpath where you want to Download the File
// Status String from Server
public static string DownloadFile(string FtpUrl,string FileNameToDownload,
string userName, string password,string tempDirPath)
{
string ResponseDescription = "";
string PureFileName = new FileInfo(FileNameToDownload).Name;
string DownloadedFilePath = tempDirPath+"/"+PureFileName;
string downloadUrl = String.Format("{0}/{1}", FtpUrl, FileNameToDownload);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(downloadUrl);
req.Method = WebRequestMethods.Ftp.DownloadFile;
req.Credentials = new NetworkCredential(userName, password);
req.UseBinary = true;
req.Proxy = null;
try
{
FtpWebResponse response = (FtpWebResponse)req.GetResponse();
Stream stream = response.GetResponseStream();
byte[] buffer = new byte[2048];
FileStream fs = new FileStream(DownloadedFilePath, FileMode.Create);
int ReadCount = stream.Read(buffer, 0, buffer.Length);
while (ReadCount > 0)
{
fs.Write(buffer, 0, ReadCount);
ReadCount = stream.Read(buffer, 0, buffer.Length);
}
ResponseDescription = response.StatusDescription;
fs.Close();
stream.Close();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
return ResponseDescription;
}

You might also like