Imports System.Net

Imports System.Threading

''' <summary>

''' Upload file to FTP Server.

''' </summary>

''' <remarks></remarks>

Private Sub Upload()

Dim fileInf As FileInfo = New FileInfo(Server.MapPath("~/images/ ") & Session("FileName"))

Dim uri As String = ("ftp://" _

+ (ftpServerIP + ("/folder path/" + fileInf.Name)))

Dim reqFTP As FtpWebRequest

' Create FtpWebRequest object from the Uri provided

reqFTP = CType(FtpWebRequest.Create(New Uri((ftp:// + (ftpServerIP + ("//folder path /" + fileInf.Name))))), FtpWebRequest)

' Provide the WebPermission Credintials

reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword)

' By default KeepAlive is true, where the control connection is not closed

' after a command is executed.

reqFTP.KeepAlive = False

' Specify the command to be executed.

reqFTP.Method = WebRequestMethods.Ftp.UploadFile

' Specify the data transfer type.

reqFTP.UseBinary = True

' 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 = New Byte((buffLength) - 1) {}

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

End Sub

''' <summary>

''' Download file from FTP Server

''' </summary>

''' <remarks></remarks>

Private Sub Download()

Try

Dim request As FtpWebRequest = CType(FtpWebRequest.Create(New Uri(("ftp://" + (ftpServerIP + ("/folder path/" + Session("FileName") + ".flv"))))), FtpWebRequest)

Dim streamFile As Stream

request.Method = WebRequestMethods.Ftp.DownloadFile

request.Credentials = New NetworkCredential(ftpUserID, ftpPassword)

request.UsePassive = True

request.UseBinary = False

request.KeepAlive = False

'close the connection when done

'Streams

Dim response As FtpWebResponse = CType(request.GetResponse, FtpWebResponse)

Dim reader As Stream = response.GetResponseStream

Dim buffer() As Byte = New Byte((1024) - 1) {}

streamFile = File.Create(Server.MapPath("~/images/ ") & Session("FileName") + ".flv")

While True

Dim bytesRead As Integer = reader.Read(buffer, 0, buffer.Length)

If (bytesRead = 0) Then

Exit While

Else

streamFile.Write(buffer, 0, bytesRead)

End If

End While

streamFile.Close()

Catch ex As Exception

'MessageBox.Show(ex.Message, "Problem downloading file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

End Try

End Sub