tags:

views:

175

answers:

2

I'm trying to send a file using ftp. I have the following code:

string server = "x.x.x.x";  // Just the IP Address 

FileStream stream = File.OpenRead(filename);
byte[] buffer = new byte[stream.Length];

WebRequest request = WebRequest.Create("ftp://" + server);
request.Method = WebRequestMethods.Ftp.UploadFile;            
request.Credentials = new NetworkCredential(username, password);

Stream reqStream = request.GetRequestStream(); // This line fails
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();

But when I run it, I get the following error:

The requested URI is invalid for this FTP command.

Please can anyone tell me why? Am I using this incorrectly?

+1  A: 

I think you need to specify the path and filename you're uploading too, so I think it should be either of:

WebRequest request = WebRequest.Create("ftp://" + server + "/");

WebRequest request = WebRequest.Create("ftp://" + server + "/filename.ext");
ho1
The first gave the same error, but once the file name was specified, it worked fine - thanks
pm_2
A: 

Hi,

when I had to use ftp method, i had to set some flags on request object, without it the function did not work:

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath);
        request.KeepAlive = true/false;
        request.UsePassive = true/false;
        request.UseBinary = xxx;

These flags depend on the server, if you have no access to the server then you cannot know what to use here, but you can test and see what works in your configuration.

And file name is probably missing at the end of URI, so that the server knows where to save uploaded file.

Those flags do not appear to be properties of the WebRequest object
pm_2
@pm_2: sorry, I forgot to add one line where request object is created. It is actually an object of FtpWebRequest type. I updated the answer to show it.