Hi all,
I have a really irritating issue trying to upload files to my clients FTP Server. I can create directories fine, I just cant upload. Also, I can upload fine using FileZilla, so I must be missing something, probably obvious!
The problem is with the statement - Dim strm As Stream = reqFTP.GetRequestStream().
Any help would be greatly appreciated!
Here is the code:
I have a really irritating issue trying to upload files to my clients FTP Server. I can create directories fine, I just cant upload. Also, I can upload fine using FileZilla, so I must be missing something, probably obvious!
The problem is with the statement - Dim strm As Stream = reqFTP.GetRequestStream().
Any help would be greatly appreciated!
Here is the code:
VB.NET:
Dim fileInf As FileInfo = New FileInfo(strFileName)
Dim uril As String = URI & "/" & strDirectory & "/" & strFileName
Dim reqFTP As FtpWebRequest
' Create FtpWebRequest object from the Uri provided
reqFTP = FtpWebRequest.Create(New Uri(uril))
' Provide the WebPermission Credintials
reqFTP.Credentials = New NetworkCredential(Login, Pass)
' By default KeepAlive is true, where the control connection is
' not closed after a command is executed.
reqFTP.KeepAlive = True
' Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile
' Specify the data transfer type.
reqFTP.UseBinary = True
reqFTP.UsePassive = False
' Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length
' The buffer size is set to 2kb
Dim buffLength As Integer = 2048
Dim buff() As Byte
Dim contentLen As Integer
' Opens a file stream (System.IO.FileStream) to read
' the file to be uploaded
Dim fs As FileStream = fileInf.OpenRead()
Try
' Stream to which the file to be upload is written
Dim strm As Stream = reqFTP.GetRequestStream()
' Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength)
' Till Stream content ends
While contentLen <> 0
' Write Content from the file stream to the
' FTP Upload Stream
strm.Write(buff, 0, contentLen)
contentLen = fs.Read(buff, 0, buffLength)
End While
' Close the file stream and the Request Stream
strm.Close()
fs.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Upload Error")
End Try