C#
FTP
file upload
programming
tutorial

Upload file to FTP using C

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Uploading a file to an FTP server from C# usually means creating an FTP request, streaming the file contents into the request body, and checking the server response. The basic workflow is simple, but it is important to be precise about credentials, paths, transfer mode, and the difference between FTP, FTPS, and SFTP.

Know Which Protocol You Actually Need

Before writing code, confirm the server protocol. Traditional FTP and FTPS use FTP commands. SFTP is a different protocol built on SSH and cannot be handled with FtpWebRequest.

If the server documentation says SFTP, use an SSH-based library instead of FTP code. If it says FTP or FTPS, a classic FTP request is appropriate.

Basic Upload With FtpWebRequest

The following example uploads a local file to an FTP server using C#:

csharp
1using System;
2using System.IO;
3using System.Net;
4
5public static class FtpUploader
6{
7    public static void UploadFile(
8        string serverUri,
9        string username,
10        string password,
11        string localFilePath,
12        string remoteFileName)
13    {
14        var request = (FtpWebRequest)WebRequest.Create($"{serverUri}/{remoteFileName}");
15        request.Method = WebRequestMethods.Ftp.UploadFile;
16        request.Credentials = new NetworkCredential(username, password);
17        request.UseBinary = true;
18        request.UsePassive = true;
19        request.KeepAlive = false;
20
21        byte[] fileContents = File.ReadAllBytes(localFilePath);
22        request.ContentLength = fileContents.Length;
23
24        using (Stream requestStream = request.GetRequestStream())
25        {
26            requestStream.Write(fileContents, 0, fileContents.Length);
27        }
28
29        using (var response = (FtpWebResponse)request.GetResponse())
30        {
31            Console.WriteLine($"Upload complete: {response.StatusDescription}");
32        }
33    }
34}

A minimal call looks like this:

csharp
1FtpUploader.UploadFile(
2    "ftp://ftp.example.com/uploads",
3    "demo-user",
4    "secret-password",
5    @"C:\temp\report.csv",
6    "report.csv");

This example reads the file into memory, which is fine for smaller uploads. For large files, stream directly from disk.

Streaming Large Files

For larger uploads, opening the local file as a stream avoids loading the whole file into memory at once:

csharp
1using (FileStream fileStream = File.OpenRead(localFilePath))
2using (Stream requestStream = request.GetRequestStream())
3{
4    fileStream.CopyTo(requestStream);
5}

That change is small but important for memory usage when uploads become large or frequent.

Handling Errors and Server Responses

FTP uploads fail for predictable reasons: wrong credentials, a bad remote path, passive mode issues, missing write permissions, or firewall restrictions. Wrap the request in a try and catch WebException so you can inspect the server response.

csharp
1try
2{
3    FtpUploader.UploadFile(
4        "ftp://ftp.example.com/uploads",
5        "demo-user",
6        "secret-password",
7        @"C:\temp\report.csv",
8        "report.csv");
9}
10catch (WebException ex)
11{
12    if (ex.Response is FtpWebResponse ftpResponse)
13    {
14        Console.WriteLine($"FTP error: {ftpResponse.StatusDescription}");
15    }
16    else
17    {
18        Console.WriteLine(ex.Message);
19    }
20}

Good error reporting saves a lot of time because FTP failures often look identical until you inspect the server status text.

Security and Deployment Notes

Do not hardcode credentials in source files. Load them from environment variables, a secret store, or encrypted configuration.

Also prefer secure transport when the server supports it. Plain FTP sends credentials and data in a form that is much easier to intercept than secure alternatives.

Finally, confirm the remote path format expected by the server. Some servers want absolute paths, others want paths relative to the login directory. A path mistake often looks like a permission problem until you examine the response carefully.

Common Pitfalls

The most common mistake is trying FTP code against an SFTP server. Those protocols are not interchangeable.

Another issue is forgetting to use the correct URI format. The path should usually start with ftp://, and the final segment must be the remote filename you want to create.

Developers also run into binary-versus-text problems. For images, archives, or other non-text files, keep UseBinary = true.

Summary

  • Confirm whether the server uses FTP, FTPS, or SFTP before choosing an API.
  • Use FtpWebRequest to upload files to FTP or FTPS servers from C#.
  • Stream large files instead of reading everything into memory.
  • Catch WebException and inspect the FTP response for useful diagnostics.
  • Keep credentials out of source code and prefer secure transport when available.

Course illustration
Course illustration

All Rights Reserved.