Reputation: 16705
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?
Upvotes: 6
Views: 12263
Reputation: 659
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.
Upvotes: 0
Reputation: 55059
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");
Upvotes: 10