Introduction
using this article i have demonstrate how to upload text file on ftp server.
Background
I am programmer having 3+ year of experience in web as well as window development.
Using the code
upload a file using FTP details, one should know the server’s FTP URL, FTP username and FTP password and specific FTP folder which is hold all file in ftp
I have set all ftp and file details from database. Code given blow
IDictionary<string, string> ftpDetails = new Dictionary<string, string>();
ftpDetails.Add("FTPUrl", dataSet.Tables[2].Rows[0]["FtpUrl"].ToString());
ftpDetails.Add("FTPUserId", dataSet.Tables[2].Rows[0]["FtpUserId"].ToString());
ftpDetails.Add("FTPPassword", dataSet.Tables[2].Rows[0]["FtpPassword"].ToString());
ftpDetails.Add("FTPFolderName", dataSet.Tables[2].Rows[0]["FolderName"].ToString());
ftpDetails.Add("FileName", "FileName");
All value set in dictonary objects after then call method which is implented the ftp uploading funtionality.
UploadFileOnFtp(ftpDetails);
I have implemented the funtionality for file uploading on ftp server.
private static void UploadFileOnFtp(IDictionary<string, string> ftpDetails)
{
try
{
FileInfo fileInfo = new FileInfo(ftpDetails["FileName"]);
string uri = "ftp://" + ftpDetails["FTPUrl"] + "/" + ftpDetails["FTPFolderName"] + "/" + fileInfo.Name;
FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpWebRequest.Credentials = new NetworkCredential(ftpDetails["FTPUserId"], ftpDetails["FTPPassword"]);
ftpWebRequest.KeepAlive = false;
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpWebRequest.UseBinary = true;
ftpWebRequest.ContentLength = fileInfo.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fileStream = fileInfo.OpenRead();
Stream stream = ftpWebRequest.GetRequestStream();
contentLen = fileStream.Read(buff, 0, buffLength);
while (contentLen != 0)
{
stream.Write(buff, 0, contentLen);
contentLen = fileStream.Read(buff, 0, buffLength);
}
stream.Close();
fileStream.Close();
}
catch (Exception ex)
{
LogManager.WriteErrorLog(ex);
throw;
}
finally
{
LogManager.WriteTraceLog(trace.ToString());
}
}
The final process whichwill be calling the above procedure by using the below
code on whatever event you want the operation to be performed
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.