Search code examples
c#ftpurlencodeurl-encoding

C# Absolute URI removes ".." from URL


I have to upload a file via FTP to ftp://ftp.remoteServer.com

My root directory on remoteServer contains an "upload" and a "download" folder. I need to put my file in the "upload" directory. But on log in, the server automatically puts me in the "download" folder.

I tried doing this:

string serverTarget = "ftp://ftp.remoteServer.com/";
serverTarget += "../upload/myfile.txt";
Uri target = new Uri(serverTarget);
FTPWebRequest ftp = (FTPWebRequest)FtpWebRequest.Create(target);

using(Stream requestStream = ftp.GetRequestStream()) {
    // Do upload here
}

This code fails with: (550) File unavailable (e.g., file not found, no access) I debugged the code, and target.AbsoluteUri returns as ftp://ftp.remoteServer.com/upload instead of ftp://ftp.remoteServer.com/../upload (missing the ..)

If I put ftp://ftp.remoteServer.com/../upload in a browser, I can log in and verify this is the correct place where I want to put my file.

How can I get the FTPWebRequest to go to the correct place?


Solution

  • I believe you can encode the dots as %2E to keep the dots in your URI.

    So something like:

    string serverTarget = "ftp://ftp.remoteServer.com/%2E%2E/upload/myfile.txt";