Monday, September 13, 2010

Understand FTP with asp.net in 5 minutes part-Ist

Hello Friends,
Today I am going to share you new thing in asp.net 2.0 which is “FTP object”. Earlier in dot net 1.1 versions there is many problem to handle “FTP”, means if you want to upload or download a file then you have to do socket programming.
Which I don’t like really but after 2.0 this problem is resolved dot net provide great solution which allow us to connect with ftp and do operation.
I will explain you with my example. So today we took how to upload a file on ftp.
For this I have design following page.


On this page there is 3 text box & file upload control basically which is
txtFtp – which keep ftp url like ftp://myftp.com
txtUserName – which keep ftp username for credential
txtPassword – which keep ftp password for credential
ctrlFlup – which keep path from where we have to upload file.

There is a button which is btnUpload on page. On click of this page we are going to upload the selected file.
So in code behind our first and most important statement is import J
Just see below code
Imports System.Net
Imports System
Imports System.Net
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Try
Dim ftpRes As FtpWebRequest = DirectCast(FtpWebRequest.Create(Me.txtFtp.Text & “/” & Me.ctrlflUp.FileName), FtpWebRequest)
ftpRes.Credentials = New NetworkCredential(Me.txtUserName.Text, Me.txtPassword.Text)
ftpRes.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim b As Byte() = Me.ctrlflUp.FileBytes
Dim fstream As System.IO.Stream = ftpRes.GetRequestStream()
fstream.Write(b, 0, b.Length)
fstream.Close()
fstream.Dispose()
Dim myresponse As FtpWebResponse = DirectCast(ftpRes.GetResponse, FtpWebResponse)
Response.Write(myresponse.StatusDescription)
Catch eh As System.Net.WebException
Response.Write(eh.Message)
Catch ex As Exception
Throw ex
End Try
End Sub
End Class

Here I will explain you the ftpwebrequest is the object which interact with FTP.
So when button upload is click we first create a object of ftp web request.
Once the object is created its credential is required for which we create a new network credential and just pass username & password.
After this you will find another important statement which is Web Request method.
Basically its notify that what user want to do import file, upload file, change directory etc.
In Next step what ever the file which we want to upload just convert that in byte array.
By file upload object as shown below
Dim b as byte() = me.ctrlflUp.fileBytes()
Once it done we just create a stream to write file on ftp.
So we just create a IO stream and write file on ftp.
To get response from Ftp as you know FTP has special command and status so we use ftp web response object.
If there is proper status then we can say our file is uploaded successfully.
In next Session we are going to download file from ftp with the help of this object.
Till then enjoy programming
Enjoy ftp.

Your friend
Rajat Jaiswal
Kindle Wireless Reading Device, Wi-Fi, 6" Display, Graphite - Latest Generation