Thursday, June 5, 2008

FTP Upload in VB.NET

This is for Source File(ftpupload.aspx.vb)
Imports System
Imports System.Text
Imports System.Net
Imports system.Data
Imports System.IO
Imports System.Uri
Partial Class ftpupload
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub upload_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles upload.ServerClick
Dim fi As FileInfo
fi = New FileInfo(fileupload.Value)
''''''''''''''''''''''FTP UPLOAD STARTS HERE''''''''''''''''''''''''''''''''
'''''''''''''''''' Get the object used to communicate with the server'''''''''''''''''''''''.
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://URL/www/FOLDERNAME/" & fi.Name), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
''''''''''''''''''This example assumes the FTP site uses anonymous logon'''''''''''''''''''''''''''''.
request.Credentials = New NetworkCredential("USERNAME", "PASSWORD")
'''''''''''''''''''Getting the filename ''''''''''''''''''''''''''''''''''''''''''''''
Dim selectedFile As HttpPostedFile = fileupload.PostedFile
''''''''''''''''''''Getting the file conetnt length'''''''''''''''''''''''''
Dim fileLength As Integer = selectedFile.ContentLength
Dim binarydata(fileLength) As Byte
'''''''''''''''''''''Reading data from the file''''''''''''''''''''''
selectedFile.InputStream.Read(binarydata, 0, fileLength)
Dim requestStream As Stream = request.GetRequestStream()
''''''''''''''''''''''''Writing data to the file''''''''''''''''''''''''
requestStream.Write(binarydata, 0, fileLength)
requestStream.Close()
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription)
response.Close()
''''''''''''''''''''''FTP UPLOAD ENDS HERE''''''''''''''''''''''''''''''''
End Sub
End Class

This for Design File(ftpupload.aspx)
Page Tag format ...
Page Language="VB" AutoEventWireup="false" CodeFile="ftpupload.aspx.vb" Inherits="ftpupload" Async="true" Strict="false"
Write Here HTML/BODY ..... tags Stars Here
One file type control with name "fileupload"
Ex:

One upload button with name "upload"
Ex:

Write Here HTML/BODY ..... tags Ends Here

No comments: