0

The error I am getting is web exception. The requested URI is invalid for this FTP command.

I am unsure as how to fix it. Anyone got any idea's? Thanks

private void SendFile(FileInfo file)
        {
            Console.WriteLine("ftp://" + ipAddressTextField.Text);
            // Get the object used to communicate with the server.
            string ftp = "ftp://" + ipAddressTextField.Text;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential(usernameTextField.Text, passwordTextField.Text);


            // Copy the contents of the file to the request stream.
            byte[] fileContents = File.ReadAllBytes(file.FullName);
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

            response.Close();
        }
user3774466
  • 23
  • 1
  • 4

1 Answers1

1

Seems you're not setting a destination filename for the upload so the server has no file name to use for the uploaded file.

You could just set it the same as the source file name using something like;

string ftp = "ftp://" + ipAddressTextField.Text + "/" + file.Name;
Console.WriteLine(ftp);

To do a slightly more robust creation of the Uri in case that the file names may have special characters, you could use the Uri class to build it, and pass that in to WebRequest.Create instead.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294