In this article I will explain how to programmatically upload files to FTP Web Server in ASP.Net using C# and VB.Net.
Files will be programmatically uploaded to FTP Web Server using the ASP.Net FileUpload control.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control, a Button and a Label.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button Text="Upload to FTP" runat="server" OnClick="FTPUpload" />
<hr />
<asp:Label ID="lblMessage" runat="server" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Net;
using System.Text;
 
VB.Net
Imports System.IO
Imports System.Net
Imports System.Text
 
 
Uploading Files to FTP Server programmatically in ASP.Net
When the Upload Button is clicked the following event handler is executed. Inside this event handler, the File is fetched from the FileUpload and converted to array of bytes.
Finally the array of bytes is uploaded to the FTP Web Server using FtpWebRequest class.
C#
protected void FTPUpload(object sender, EventArgs e)
{
    //FTP Server URL.
    string ftp = "ftp://yourserver.com/";
 
    //FTP Folder name. Leave blank if you want to upload to root folder.
    string ftpFolder = "Uploads/";
 
    byte[] fileBytes = null;
 
    //Read the FileName and convert it to Byte array.
    string fileName = Path.GetFileName(FileUpload1.FileName);
    using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream))
    {
        fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
        fileStream.Close();
    }
 
    try
    {
        //Create FTP Request.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
        request.Method = WebRequestMethods.Ftp.UploadFile;
 
       //Enter FTP Server credentials.
        request.Credentials = new NetworkCredential("UserName", "Password");
        request.ContentLength = fileBytes.Length;
        request.UsePassive = true;
        request.UseBinary = true;
        request.ServicePoint.ConnectionLimit = fileBytes.Length;
        request.EnableSsl = false;
 
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(fileBytes, 0, fileBytes.Length);
            requestStream.Close();
        }
 
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
 
        lblMessage.Text += fileName + " uploaded.<br />";
        response.Close();
    }
    catch (WebException ex)
    {
        throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
    }
}
 
VB.Net
Protected Sub FTPUpload(sender As Object, e As EventArgs)
    'FTP Server URL.
    Dim ftp As String = "ftp://yourserver.com/"
 
    'FTP Folder name. Leave blank if you want to upload to root folder.
    Dim ftpFolder As String = "Uploads/"
 
    Dim fileBytes As Byte() = Nothing
 
    'Read the FileName and convert it to Byte array.
    Dim fileName As String = Path.GetFileName(FileUpload1.FileName)
    Using fileStream As New StreamReader(FileUpload1.PostedFile.InputStream)
        fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())
        fileStream.Close()
    End Using
 
    Try
        'Create FTP Request.
        Dim request As FtpWebRequest = DirectCast(WebRequest.Create(ftp & ftpFolder & fileName), FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.UploadFile
 
        'Enter FTP Server credentials.
        request.Credentials = New NetworkCredential("UserName", "Password")
        request.ContentLength = fileBytes.Length
        request.UsePassive = True
        request.UseBinary = True
        request.ServicePoint.ConnectionLimit = fileBytes.Length
        request.EnableSsl = False
 
        Using requestStream As Stream = request.GetRequestStream()
            requestStream.Write(fileBytes, 0, fileBytes.Length)
            requestStream.Close()
        End Using
 
        Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
 
        lblMessage.Text &= fileName & " uploaded.<br />"
        response.Close()
    Catch ex As WebException
        Throw New Exception(TryCast(ex.Response, FtpWebResponse).StatusDescription)
    End Try
End Sub
 
 
Downloads