Tuesday, 15 May 2012

C# Programming : Upload file on FTP server using c#

You are able to upload a file on FTP server from your application.
Using FtpWebRequest and FtpWebResponse classes executing File Transfer Protocol (FTP) commands from your Web page easy.
In this example we are using System.Net and System.IO namespaces.
Here is exmple for how to upload file on FTP server.

C# Example :
string filename = Server.MapPath("file1.txt");
string ftpServerIP = "ftp.demo.com/";
string ftpUserName = "dummy";
string ftpPassword = "dummy";

FileInfo objFile = new FileInfo(filename);
FtpWebRequest objFTPRequest;

// Create FtpWebRequest object 
objFTPRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + objFile.Name));

// Set Credintials
objFTPRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

// By default KeepAlive is true, where the control connection is 
// not closed after a command is executed.
objFTPRequest.KeepAlive = false;

// Set the data transfer type.
objFTPRequest.UseBinary = true;

// Set content length
objFTPRequest.ContentLength = objFile.Length;

// Set request method
objFTPRequest.Method = WebRequestMethods.Ftp.UploadFile;

// Set buffer size
int intBufferLength = 16 * 1024;
byte[] objBuffer = new byte[intBufferLength];
        
// Opens a file to read
FileStream objFileStream = objFile.OpenRead();

try
{
    // Get Stream of the file
    Stream objStream = objFTPRequest.GetRequestStream();

    int len = 0;

    while ((len = objFileStream.Read(objBuffer, 0, intBufferLength)) != 0)
    {
        // Write file Content 
        objStream.Write(objBuffer, 0, len);
                
    }
                        
    objStream.Close();
    objFileStream.Close();
}
catch (Exception ex)
{
    throw ex;
}

In this example set appropriate Server and Set credentials if required.

VB.net Example :
Dim filename As String = Server.MapPath("file1.txt")
Dim ftpServerIP As String = "ftp.demo.com/"
Dim ftpUserName As String = "dummy"
Dim ftpPassword As String = "dummy"

Dim objFile As New FileInfo(filename)
Dim objFTPRequest As FtpWebRequest

' Create FtpWebRequest object 
objFTPRequest = DirectCast(FtpWebRequest.Create(New Uri(("ftp://" & ftpServerIP & "/") + objFile.Name)), FtpWebRequest)

' Set Credintials
objFTPRequest.Credentials = New NetworkCredential(ftpUserName, ftpPassword)

' By default KeepAlive is true, where the control connection is 
' not closed after a command is executed.
objFTPRequest.KeepAlive = False

' Set the data transfer type.
objFTPRequest.UseBinary = True

' Set content length
objFTPRequest.ContentLength = objFile.Length

' Set request method
objFTPRequest.Method = WebRequestMethods.Ftp.UploadFile

' Set buffer size
Dim intBufferLength As Integer = 16 * 1024
Dim objBuffer As Byte() = New Byte(intBufferLength - 1) {}

' Opens a file to read
Dim objFileStream As FileStream = objFile.OpenRead()

Try
    ' Get Stream of the file
    Dim objStream As Stream = objFTPRequest.GetRequestStream()

    Dim len As Integer = 0

    len = objFileStream.Read(objBuffer, 0, intBufferLength)
    While len <> 0
         ' Write file Content 
         objStream.Write(objBuffer, 0, len)
         len = objFileStream.Read(objBuffer, 0, intBufferLength)
    End While
    objStream.Close()
    objFileStream.Close()
Catch ex As Exception
    Throw ex
End Try

No comments:

Post a Comment