Create Page Default.aspx.vb
Write Method
Public Sub FTPFileTransfer()
Dim objFtp As New FTP.FtpClass()
Dim filePath As String = Server.MapPath("download/test.txt")
objFtp.FtpUrl = "ftp://www.yoursite.com/FolderName"
objFtp.Ftppassword = "Ftppassword"
objFtp.Ftpusername = "Ftpusername"
If objFtp.UploadFileToFTP(filePath) Then
lblMessage.Text = "File has been uploaded on the server <br/> Check the uploaded file on http://yoursite.com/" & Path.GetFileName(filePath)
Else
Console.Write(objFtp.FtpException.Message)
End If
End Sub
Create Class FtpClass
Write Method
Imports System.Reflection
Imports System.Data
Imports System.IO
Imports System.Net
Imports System.Text
Public Class FtpClass
Public Property FtpUrl() As String
Get
Return m_FtpUrl
End Get
Set(value As String)
m_FtpUrl = value
End Set
End Property
Private m_FtpUrl As String
Public Property Ftpusername() As String
Get
Return m_Ftpusername
End Get
Set(value As String)
m_Ftpusername = value
End Set
End Property
Private m_Ftpusername As String
Public Property Ftppassword() As String
Get
Return m_Ftppassword
End Get
Set(value As String)
m_Ftppassword = value
End Set
End Property
Private m_Ftppassword As String
Public Property FtpException() As Exception
Get
Return m_FtpException
End Get
Set(value As Exception)
m_FtpException = value
End Set
End Property
Private m_FtpException As Exception
Public Function UploadFileToFTP(source As String) As Boolean
Try
Dim filename As String = Path.GetFileName(source)
Dim ftpfullpath As String = FtpUrl
' +"/aceafrica";
Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create(ftpfullpath & "/" & filename), FtpWebRequest)
ftp.Credentials = New NetworkCredential(Ftpusername, Ftppassword)
ftp.KeepAlive = True
ftp.UseBinary = True
ftp.Method = WebRequestMethods.Ftp.UploadFile
Dim fs As FileStream = File.OpenRead(source)
Dim buffer As Byte() = New Byte(fs.Length - 1) {}
fs.Read(buffer, 0, buffer.Length)
fs.Close()
Dim ftpstream As Stream = ftp.GetRequestStream()
ftpstream.Write(buffer, 0, buffer.Length)
ftpstream.Close()
Return True
Catch ex As Exception
FtpException = ex
Return False
End Try
End Function
Public Function GetFileList(Directory As String) As Boolean
' Public Function GetFileList(Directory As String) As List(Of String)
Dim Files As New List(Of String)()
Dim filename As String = Path.GetFileName(Directory)
Dim ftpfullpath As String = FtpUrl
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(New Uri(ftpfullpath & "/" & filename)), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.ListDirectory
request.Credentials = New NetworkCredential(Ftpusername, Ftppassword)
' Is this correct?
' request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Password); // Or may be this one?
request.UseBinary = False
request.UsePassive = True
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
Dim CurrentLine As String = reader.ReadLine()
While Not String.IsNullOrEmpty(CurrentLine)
Files.Add(CurrentLine)
CurrentLine = reader.ReadLine()
End While
reader.Close()
response.Close()
Return True
End Function
End Class