How to: Upload Files with FTP

This sample shows how to upload a file to an FTP server.

Example

C#
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();
            }
        }
    }
}
Compiling the Code

This example requires:

  • References to the System.Net namespace.

Tags :


Community Content

Jamie Miley
This doesn't seem to work for many files without a small change

This encoding method will work for text that is encoded utf-8 but if you want to do any file you should use:

byte[] fileContents = file.ReadAllBytes("testfile.txt");

NOT:

StreamReader sourceStream = new StreamReader("testfile.txt");
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

Tags : ftp c# excel

Patrick John Collins
ContentLength property is superfluous
Note, that the ContentLength property is superfluous and does not need to be set.

The line :
request.ContentLength = fileContents.Length;
could be removed from the example.

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.contentlength.aspx

"The ContentLength property is provided only for compatibility with other implementations of the WebRequest and WebResponse classes. There is no reason to use ContentLength."
Tags :

Jonathan Cano Escobar
Tips

This code is very useful. Here are some tips to help you understand it, because there are things which are not so obvious.

1. When you use

FtpWebRequest UploadFileRequest = FtpWebRequest.Create(ftp://www.contoso.com/test.htm) asFtpWebRequest;

When you place the name "test.htm", this is the actual name of the file you want to upload, which is really 'creating' an empty file with that name. I had to upload an empty file. So I just put the name of the file I wanted to create in the folder I wanted it to be.

So if you want it in subfolder "folder3" You can write: Ftp://www.contoso.com/folder1/folder2/folder3/test.htm.

The file will be empty. So then you can either write into the file or place the content of another file: in the example above it would be "testfile.txt". You would obviously have to place a path here to get the file.

Tags :

Page view tracker