Hi guys,
I am facing a problem with my FTP to upload a file to the FTP Server.
I keep on getting an error "The remote server returned an error: (451) Local error in processing.
I have tried Passive mode, but with no luck. Any recommendations are welcome.
Please see my code below:
private void Upload(string filename, string ftpServerIP, string ftpUser,string ftpPassword)
{
MakeDir(filename, ftpServerIP, ftpUser, ftpPassword);
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + Txt_TransferTo.Text+ "/" + fileInf.Name;
FtpWebRequest FTPreq;
FTPreq = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + Txt_TransferTo.Text+ "/" + fileInf.Name));
FTPreq.Credentials = new NetworkCredential(ftpUser, ftpPassword);
FTPreq.Method = WebRequestMethods.Ftp.MakeDirectory;
FTPreq.KeepAlive = false;
FTPreq.Method = WebRequestMethods.Ftp.UploadFile;
FTPreq.UseBinary = true;
FTPreq.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = FTPreq.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
InfoMessage("File uploaded succesfully", 1);
}
catch (Exception ex)
{
InfoMessage(ex.Message, 0);
}
}
private void MakeDir(string dirName, string ftpServerIP, string ftpUser, string ftpPassword)
{
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch { }
}
and i call it via
Upload("filename", "ftp.XXXXX.XX.XX", "UserName", "Password");
but with no luck.
Thanks