VB.net Uploading files through ftp.

Using visual studio 2010 and server 2010. I used below function to upload my files to ftp server running on Linux. Initially ftp was not working properly on server 2008. This issue can be resolved if you set ur ftp mode to passive. in windows cmd prompt it may be done using command >quote PASV


C:\>ftp 191.254.1.42
Connected to 191.254.1.42.
220 PMC FTP server (Version 6.4/OpenBSD/Linux-ftpd-0.17) ready.
User (191.254.1.42:(none)): admin
331 Password required for admin.
Password:
230- Linux PMC 2.6.26-2-686-bigmem #1 SMP Sat Dec 26 09:26:36 UTC 2009 i686
230 User admin logged in.
ftp> put mou_summary.pdf
200 PORT command successful.
425 Can't build data connection: Connection timed out.
ftp> put mou_summary.pdf
200 PORT command successful.
425 Can't build data connection: Connection timed out.
ftp> put mou_summary.pdf
421 Timeout (900 seconds): closing control connection.
Connection closed by remote host.
ftp> bye

I created a function in vb.net to upload file via ftp with passive mode as true.
Call it like this

 'start upload

FtpUploadFileToServer("191.254.1.42", Application.StartupPath & "\pdf\" & Strings.Left(fi.Name, Len(fi.Name) - 4) & ".pdf", "/home/admin", , "admin", "admin&123", , True)

 
 Private Function FtpUploadFileToServer(ByVal pServer As String, _


ByVal pUploadPathAndFileName As String, _


Optional ByVal pTargetPath As String = "", _


Optional ByVal pTargetFileName As String = "", _


Optional ByVal pUserName As String = "", _


Optional ByVal pPassword As String = "", _


Optional ByVal pPort As Integer = 21, _


Optional ByVal pUsePassive As Boolean = False) As Boolean


Dim objUploadStream As FileStream = Nothing


Dim objRequest As FtpWebRequest = Nothing


Dim objResponse As FtpWebResponse = Nothing


Dim objRequestStream As Stream = Nothing


Try


lblStatus.Text = lblStatus.Text & " initiating upload " & pUploadPathAndFileName


objUploadStream = File.OpenRead(pUploadPathAndFileName)


Dim bytBuffer(CType(objUploadStream.Length, Integer)) As Byte


objUploadStream.Read(bytBuffer, 0, bytBuffer.Length)






If pTargetFileName.Length = 0 Then


pTargetFileName = IO.Path.GetFileName(objUploadStream.Name)


End If


Dim strUrl As String = String.Format("ftp://{0}:{1}/{2}/{3}", _


pServer, pPort, pTargetPath, pTargetFileName)


objRequest = CType(FtpWebRequest.Create(strUrl), FtpWebRequest)


If pUserName.Length > 0 And pPassword.Length > 0 Then


objRequest.Credentials = New NetworkCredential(pUserName, pPassword)


End If


objRequest.Method = WebRequestMethods.Ftp.UploadFile


objRequest.Proxy = Nothing


objRequest.KeepAlive = False


objRequest.UsePassive = pUsePassive






objRequestStream = objRequest.GetRequestStream()


objRequestStream.Write(bytBuffer, 0, bytBuffer.Length)


objRequestStream.Close()






objResponse = CType(objRequest.GetResponse, FtpWebResponse)


'MsgBox(objResponse.StatusDescription)


lblStatus.Text = lblStatus.Text & " - " & objResponse.StatusDescription & " at " & Now.TimeOfDay.ToString & " ->"






Catch ex As Exception


'MsgBox(ex.Message)


lblStatus.Text = lblStatus.Text & ex.Message & "->"


Finally


Try


If Not objRequestStream Is Nothing Then


objRequestStream.Close()


End If


If Not objUploadStream Is Nothing Then


objUploadStream.Close()


objUploadStream.Dispose()


End If


If Not objRequest Is Nothing Then


objRequest = Nothing


End If






Catch ex As Exception


'MsgBox(ex.Message)


lblStatus.Text = lblStatus.Text & vbCrLf & ex.Message


End Try


End Try


End Function

Comments

Asava Samuel said…
This has to be my favorite FTP Library for VB.NET:
https://www.kellermansoftware.com/p-39-net-ftp-library.aspx

Most Viewed Post

solution for facebook static.ak.fbcdn.net waiting

Connect Sony XPLOD MEX-BT2500 Bluetooth CD Receiver with ur mobile

Crystal Report : Show data horizontally (Left to right) i.e. columns as rows

Google+ Followers