Posted By

Sadeveloper on 12/26/13


Tagged

VBNETuploadFTP


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

seth9009
D4RKCODE


Upload file to FTP server


 / Published in: VB.NET
 

URL: http://www.softafzar.net/thread782.html/

Upload file to FTP server

  1. ''' <summary>
  2. ''' Methods to upload file to FTP Server
  3. ''' </summary>
  4. ''' <param name="_FileName">local source file name</param>
  5. ''' <param name="_UploadPath">Upload FTP path including Host name</param>
  6. ''' <param name="_FTPUser">FTP login username</param>
  7. ''' <param name="_FTPPass">FTP login password</param>
  8.  
  9. Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String, ByVal _FTPUser As String, ByVal _FTPPass As String)
  10. Dim _FileInfo As New System.IO.FileInfo(_FileName)
  11.  
  12. ' Create FtpWebRequest object from the Uri provided
  13. Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
  14.  
  15. ' Provide the WebPermission Credintials
  16. _FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
  17.  
  18. ' By default KeepAlive is true, where the control connection is not closed
  19. ' after a command is executed.
  20. _FtpWebRequest.KeepAlive = False
  21.  
  22. ' set timeout for 20 seconds
  23. _FtpWebRequest.Timeout = 20000
  24.  
  25. ' Specify the command to be executed.
  26. _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
  27.  
  28. ' Specify the data transfer type.
  29. _FtpWebRequest.UseBinary = True
  30.  
  31. ' Notify the server about the size of the uploaded file
  32. _FtpWebRequest.ContentLength = _FileInfo.Length
  33.  
  34. ' The buffer size is set to 2kb
  35. Dim buffLength As Integer = 2048
  36. Dim buff(buffLength - 1) As Byte
  37.  
  38. ' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
  39. Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
  40.  
  41. Try
  42. ' Stream to which the file to be upload is written
  43. Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
  44.  
  45. ' Read from the file stream 2kb at a time
  46. Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
  47.  
  48. ' Till Stream content ends
  49. Do While contentLen <> 0
  50. ' Write Content from the file stream to the FTP Upload Stream
  51. _Stream.Write(buff, 0, contentLen)
  52. contentLen = _FileStream.Read(buff, 0, buffLength)
  53. Loop
  54.  
  55. ' Close the file stream and the Request Stream
  56. _Stream.Close()
  57. _Stream.Dispose()
  58. _FileStream.Close()
  59. _FileStream.Dispose()
  60. Catch ex As Exception
  61. MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  62. End Try
  63. End Sub
  64.  
  65.  
  66. HOW TO USE:
  67.  
  68. ' Upload file using FTP
  69. UploadFile("c:\UploadFile.doc", "ftp://FTPHostName/UploadPath/UploadFile.doc", "UserName", "Password")

Report this snippet  

You need to login to post a comment.