Using ftp in C # to send a file

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

string server = "xxxx"; // 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 not valid for this FTP command.

Please tell me why? Am I using this incorrectly?

+4
source share
2 answers

I think you need to specify the path and name of the file you are loading, so I think this should be either:

 WebRequest request = WebRequest.Create("ftp://" + server + "/"); WebRequest request = WebRequest.Create("ftp://" + server + "/filename.ext"); 
+8
source

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

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

These flags are server dependent, if you do not have access to the server, then you may not know what to use here, but you can check and see what works in your configuration.

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

0
source

Source: https://habr.com/ru/post/1308876/


All Articles