Created
June 3, 2014 00:41
-
-
Save sir4ju1/72cc01f4ae099e780eed to your computer and use it in GitHub Desktop.
Upload and Download file through FTP in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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