Description:-
In this Article about downloading and uploading binary files using Ftp Web Request in C#. Here we will create code to download and upload files to FTP Servers.
In this Article about downloading and uploading binary files using Ftp Web Request in C#. Here we will create code to download and upload files to FTP Servers.
We will use the
Ftp Web Request and Ftp Web Response classes. The reason to use these classes
instead of using Web Client classes is that if we use Ftp Web Request/Response
classes to upload and download files then we have more control of what is done
inside. The code I am showing here is easy to understand; most of the parameters
I have specified are commented out so you can understand easier. First you will
need two namespaces to work with FTP; they are:
using System.Net;
using System.IO;
using System.IO;
And here
are the two functions I have made to download and Upload files.
Uploading
File
//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; }
Downloading File
//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; }
1 comments:
commentsHey I know this is off topic but I was wondering if you knew
Replyof any widgets I could add to my blog that automatically tweet my newest twitter
updates. I've been looking for a plug-in like this for quite some time and
was hoping maybe you would have some experience with something
like this. Please let me know if you run into anything.
I truly enjoy reading your blog and I look forward to your new updates.
Thanks for comments.....