How to Upload File using FTP in ASP.Net 2.0

March 12, 2008 – 12:18 pm

In this article I will try to explain you the technique of uploading file using ASP.Net 2.0.

.Net Framework 2.0 came with many new features which helps the developers in solving daily routines problems/quires while developing an application either windows or desktop. Today as we have seen Web Application became the part of our life. When we are facing any difficulty regarding development we took help from web application, similarly, when we want to purchase any thing we look for e-commerce websites.

Since we are a part of 21st century and in this century we have seen many developments in technology. Technology facilitates users in many ways for example, remote universities. Student sitting on one end and university on the other, to transfer course content, lectures etc. usually FTP (File Transfer Protocol) is used. When we talk about .Net Framework 1.0 we didn’t see any facility in using FT Protocol. .Net Framework 2.0 provides us FTPWebRequest and FTPWebResponse classes. These two classes provide complete ease to developers to implement an entire FTP client right from their web application. To get more understanding about these classes consider an example that upload file from local system to ftp://localhost/. You can also transfer file to live servers by providing network credentials (UserID, PWD).

Create a public function named as uploadFileUsingFTP(). This fuction uses four parameters, CompleteFTPPath this parameter is used for passing URL of FTP, CompleteLocalPath used to receive local path. Third and Fourth parameters are UName and PWD used to validate network crediential. Code for this function is as follows:

Public Sub uploadFileUsingFTP(ByVal fileFTP As String, ByVal clientPath As String)
        Dim fileInf As FileInfo = New FileInfo(clientPath)

        ’Create a FTP Request Object and Specfiy a Complete Path and save your ftp addres
        ’User Name PWD of ftp in web config
        Dim CompleteFTPPath As String = ConfigurationManager.AppSettings("FTP") & fileFTP

        ‘If you want to access Resourse Protected You need to give User Name and PWD
        Dim UName As String = ConfigurationManager.AppSettings("UserFTP")
        Dim PWD As String = ConfigurationManager.AppSettings("PwdFTP")

        ‘Create FtpWebRequest object from Uri provided
        Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(New Uri(CompleteFTPPath)), FtpWebRequest)

        ‘Provide the WebPermission Credintials
        ftp.Credentials = New System.Net.NetworkCredential(UName, PWD)

        ‘By default KeepAlive is true
        ftp.KeepAlive = False

        ‘Specify the command to be executed
        ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile

        ‘Specify the data transfer type
        ftp.UseBinary = True

        ‘Notify the server about the size of the uploaded file
        ftp.ContentLength = fileInf.Length

        ‘Set buffer size = 2 kb
        Dim buffLength As Integer = 2048
        Dim buff(buffLength) As Byte
        Dim contentLength As Integer

        ‘Open a file stream to read the file to be uploaded
        Dim fs As FileStream = fileInf.OpenRead()

        Try
            ‘Stream to which the file to be upload is written
            Dim strm As Stream = ftp.GetRequestStream()

            ‘Read from the file stream 2kb at a time
            contentLength = fs.Read(buff, 0, buffLength)

            ‘Till Stream content ends
            While contentLength 0
                ‘Write Content from the file stream to the FTP Upload Stream
                strm.Write(buff, 0, contentLength)
                contentLength = fs.Read(buff, 0, buffLength)
            End While

            ‘Close the file stream and the Request Stream
            strm.Close()
            fs.Close()
        Catch ex As Exception
            ‘Set error handling here
       End Try
End Sub

I hope this article will be help you when you get a problem how to use the FTP in ASP.NET. Good luck for you, and thanks.


No comments yet.

Post a Comment


Anti-spam measure: please retype the above text into the box provided.