Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET2.0 C# VS2010 FTP
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
Posted 18-Jun-12 8:09am
Edited 19-Jun-12 3:41am
v4
Comments
SAKryukov at 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 at 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 at 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 at 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 at 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
jpveldtman at 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 at 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.
--SA

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 BillWoodruff 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 19 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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