Click here to Skip to main content
13,801,290 members
Rate this:
Please Sign up or sign in to vote.
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();
                Stream strm = FTPreq.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                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;
                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();

            catch { }

and i call it via
Upload("filename", "ftp.XXXXX.XX.XX", "UserName", "Password");

but with no luck.
Posted 18-Jun-12 8:09am
Updated 23-Feb-18 0:32am
Sergey Alexandrovich Kryukov 18-Jun-12 13:14pm
This is a client code, but how do you know that your FTP server works properly? Did you try to upload the same file in the same location using some available FTP server? What happens?
jpveldtman 18-Jun-12 17:25pm
Thanks for the reply, what i can add, it works if i use for example filezilla.I haven't tried on a different FTP server, If the problem is with the server i have no idea what it can be then...
jpveldtman 19-Jun-12 14:38pm
Ok, i've played around with the usepassive and keepalive, but then i am getting an error that the file doesn't exist / i don't have access. But the file does excist and i definitly do have access, i can modify the file manually etc
jpveldtman 26-Jun-12 13:32pm
I figured out that FTP is case sensitive, if i add it to a folder(which is uppercase) but i enter lower case, then it gives an error that i don't have access. Can anybody please help to see if the folder exists and adjust to the correct case?
Sergey Alexandrovich Kryukov 29-Jun-12 22:32pm
Case sensitivity depends on platform. You can test the existence using commands in FtpWebRequest; order directory list, and it will give you folders.
jpveldtman 22-Jul-12 3:54am
Thanks, I run it on Windows, but the FTP server is Linux based,
I used the directory list, so the user chooses a directory instead of typing it in, thanks for your help.
Sergey Alexandrovich Kryukov 27-Jul-12 19:15pm
Well, the problem is: even though Windows is non case-sensitive, Linux is, so, quite apparently, you need to use strict case in file names.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.181215.1 | Last Updated 23 Feb 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100