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).
To begin with this application create new website in VS2005. Add two controls one is label control and other is button control. Your .aspx source view page looks like this:
Public Sub uploadFileUsingFTP(ByVal CompleteFTPPath As String, ByVal CompleteLocalPath As String, Optional ByVal UName As String = "", Optional ByVal PWD As String = "")
'Create a FTP Request Object and Specfiy a Complete Path
Dim reqObj As FtpWebRequest = WebRequest.Create(CompleteFTPPath)
'Call A FileUpload Method of FTP Request Object
reqObj.Method = WebRequestMethods.Ftp.UploadFile
'If you want to access Resourse Protected You need to give User Name and PWD
reqObj.Credentials = New NetworkCredential(UName, PWD)
'FileStream object read file from Local Drive
Dim streamObj As FileStream = File.OpenRead(CompleteLocalPath)
'Store File in Buffer
Dim buffer(streamObj.Length) As Byte
'Read File from Buffer
streamObj.Read(buffer, 0, buffer.Length)
'Close FileStream Object Set its Value to nothing
streamObj.Close()
streamObj = Nothing
'Upload File to ftp://localHost/ set its object to nothing
reqObj.GetRequestStream().Write(buffer, 0, buffer.Length)
reqObj = Nothing
End Sub
On cmdUpload_Click function add the following code:
Protected Sub cmdUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdUpload.Click
'On button Click call the function
'and Pass Parameter Values
'Display Message on Success
uploadFileUsingFTP("ftp://localhost/ftp1.txt", "C:\Documents and Settings\Administrator\Desktop\ftp.txt")
lblMsg.Text = "File Uploaded Successfully :)"
End Sub
No comments:
Post a Comment