3

I am using the following code to learn how to load files with FTP. How do I set the path or folder into which my file will be uploaded ?

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader("testfile.txt");
            byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            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();
            }
        }
    }
}
Steam
  • 9,368
  • 27
  • 83
  • 122

1 Answers1

5

The folder is part of the URL you set when you create request: "ftp://www.contoso.com/test.htm". If you use "ftp://www.contoso.com/wibble/test.htm" then the file will be uploaded to a folder named wibble.

You may need to first use a request with Method = WebRequestMethods.Ftp.MakeDirectory to make the wibble folder if it doesn't already exist.

Ganesh Sittampalam
  • 28,821
  • 4
  • 79
  • 98
  • I was using ftp.myserver.com/myfolder. I got the error - System.UriFormatException: Invalid URI: The format of the URI could not be determined. How do I fix this ? – Steam Dec 31 '13 at 22:31
  • Did you include the `ftp://` at the front? – Ganesh Sittampalam Dec 31 '13 at 22:32
  • I made it ftp : // myserver . com / myfolder and got the error - System.Net.WebException: Unable to connect to the remote server – Steam Dec 31 '13 at 22:33
  • I purposely put all those spaces in the ftp address so that SO will not convert it into a link. – Steam Dec 31 '13 at 22:34
  • 1
    The `ftp` string has two purposes and so may be needed twice: firstly as the "protocol specifier" in the beginning `"ftp://"` and secondly it may be part of the hostname of the remote server. Your earlier example had it as part of the hostname but your latest one doesn't. Should you be using `ftp://ftp.myserver.com/myfolder` ? – Ganesh Sittampalam Dec 31 '13 at 22:35
  • Note that if you use backticks to surround the address then it won't get turned into a link. – Ganesh Sittampalam Dec 31 '13 at 22:36
  • Thanks. I did that and got a new exception :( It never happens with mozilla ftp client. - System.Net.WebException: The remote server returned an error: (553) File name not allowed. – Steam Dec 31 '13 at 22:38
  • Does `myfolder` actually exist? – Ganesh Sittampalam Dec 31 '13 at 22:38
  • Yes, I can see it in Mozilla FTP client - as / > Fold1 > Fold2 > myfolder > fold3 etc... – Steam Dec 31 '13 at 22:41
  • Not certain then - this question has some more info about that error: http://stackoverflow.com/questions/9418404/cant-connect-to-ftp-553-file-name-not-allowed – Ganesh Sittampalam Dec 31 '13 at 22:45
  • Thanks for the link. I don't know the cause of my problem. I saw the second answer and it says that the default directory might be a problem. How do I find out the default directory of my server ? – Steam Dec 31 '13 at 22:50
  • Where do files end up if you don't specify any folder? That's the default. – Ganesh Sittampalam Dec 31 '13 at 22:51
  • Okay. I got my mistake. I did not include the filename in the URL. – Steam Dec 31 '13 at 23:05
  • Oh, I missed that completely while focusing on the other issues :-) – Ganesh Sittampalam Dec 31 '13 at 23:06