Skip to content

Instantly share code, notes, and snippets.

@newpost
Forked from sir4ju1/FtpTransfer.cs
Created October 18, 2022 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newpost/0e9cc82864b91da38b25863fc6a08687 to your computer and use it in GitHub Desktop.
Save newpost/0e9cc82864b91da38b25863fc6a08687 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment