4

I am using c# in .NEt 2.0 to simply try to upload a file. Everything seems ok in the code, but it keeps failing at when I go to create a stream from the FtpWebRequest.GetRequestStream method.

Here is the code...

        FtpWebRequest ftpRequest;
        FtpWebResponse ftpResponse;

        try
        {
            string fileName = Path.GetFileName(strCompleteFilePath);
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost/" + fileName));
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpRequest.Proxy = null;
            ftpRequest.UseBinary = true;
            ftpRequest.Credentials = new NetworkCredential("myUserID", "myPW");
            ftpRequest.KeepAlive = false;

            FileInfo ff = new FileInfo(strCompleteFilePath);
            byte[] fileContents = new byte[ff.Length];

            using (FileStream fr = ff.OpenRead()) 
            {
                fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
            }

            using (Stream writer = ftpRequest.GetRequestStream())
            {
                writer.Write(fileContents, 0, fileContents.Length);
            }

            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); 
        }

And the error....

{System.Net.WebException: The remote server returned an error: (501) Syntax error in parameters or arguments.
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.InvokeRequestCallback(Object obj)
   at System.Net.CommandStream.Abort(Exception e)
   at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.GetRequestStream()
Svish
  • 152,914
  • 173
  • 462
  • 620
donde
  • 41
  • 1
  • 3
  • Sorry, the / before the file name WAS there. I accidentally took it out when posting it. But, when I run the code, and it fails, the / IS there. – donde May 05 '10 at 15:47
  • What's the server? Here's a question about an AS/400 that may help: http://stackoverflow.com/questions/1930787/ftpwebrequest-connecting-to-an-as-400 – Richard Morgan May 05 '10 at 16:37

5 Answers5

5

You are missing a / in the path.

You are going to be creating a path that is ftp://myhostmyfile.txt if your file was called "myfile.txt", which I'm guessing should be ftp://myhost/myfile.txt

Therefore just add a / to the end of the ftp://myhost string.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
4

This looks wrong:

ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost" + fileName));

Unless the contents of filename starts with a / I think you need to add one of those so it would be like:

ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost/" + fileName));
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
3

The line:

ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost" + fileName));

Might be a problem if your fileName variable doesn't include the necessary slashes.

JYelton
  • 35,664
  • 27
  • 132
  • 191
1

Try

ftpRequest.UsePassive = false;

it works for me.

Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
1

The FTP server is unhappy about the STOR command that .NET generates. Best place to look is in the log file for the server. Taking a wild guess: the path is unusual, you'd typical want to specify a directory name (like ftp://myhost/somedir/filename)

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536