Click here to Skip to main content
15,887,966 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
Upload("filename", "ftp.XXXXX.XX.XX", "UserName", "Password");

but with no luck.
Thanks
Posted
Updated 22-Feb-18 23:32pm
v4
Comments
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?
--SA
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.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900