Code to upload file to FTP Server
Check out the code; how you can upload file to specified FTP Server programatically.
//////
/// Complete physical path of the file to be uploaded
/// FTP Path
/// FTP User account name
/// FTP User password
///
private bool UploadToFTP(string strFilePath, string strFTPPath, string strUserName, string strPassword)
{
try
{
//Create a FTP Request Object and Specfiy a Complete Path
string strFileName = strFilePath.Substring(strFilePath.LastIndexOf("\\") + 1);
FtpWebRequest reqObj = (FtpWebRequest)WebRequest.Create(strFTPPath + @"/" + strFileName);
//Call A FileUpload Method of FTP Request Object
reqObj.Method = WebRequestMethods.Ftp.UploadFile;
//If you want to access Resourse Protected You need to give User Name and PWD
reqObj.Credentials = new NetworkCredential(strUserName, strPassword);
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(strFilePath);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
reqObj.ContentLength = fileContents.Length;
Stream requestStream = reqObj.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)reqObj.GetResponse();
response.Close();
}
catch (Exception Ex)
{ // report error
throw Ex;
}
return true;
}
No comments:
Post a Comment