Hi there,
I've been successfully using a BackgroundWorker to display the upload progress of a single file with the following code:
As our company now uses a proxy server, I looked for a solution and it was to move the FTP upload to either FtpWebRequest or WebClient.
I went with FtpWebRequest using the following code:
But this doesn't display any progress...
I have tried updating a ProgressBar I have placed, which would be useful as then I can customise the look of the progress, but even this doesn't move.
Any help with this would be greatly appreciated.
I'm hoping to move towards upload folders and subfolders, from what I've read you do this by recursive scans through the local folders and create them on the remote server. If anyone has recomendation on FtpWebRequest or WebClient or something else for this, please let me know as I can start working with that now.
Thanks in advance.
I've been successfully using a BackgroundWorker to display the upload progress of a single file with the following code:
vb Code:
Private Sub bgw_UploadZip_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw_UploadZip.DoWork 'Uploads complete .zip file via FTP to client location My.Computer.Network.UploadFile(System.IO.Path.GetTempPath & ZipFilename & ".zip", "ftp://ftp.domain.com/file.ext", "user", "pass", FileIO.UIOption.AllDialogs, 100, FileIO.UICancelOption.DoNothing)
As our company now uses a proxy server, I looked for a solution and it was to move the FTP upload to either FtpWebRequest or WebClient.
I went with FtpWebRequest using the following code:
vb Code:
Private Sub bgw_UploadZip_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw_UploadZip.DoWork 'Uploads complete .zip file via FTP to client location Dim FilePath As String = System.IO.Path.GetTempPath & ZipFilename & ".zip" Dim UploadPath As String = "ftp://ftp.domain.com/" & ZipFilename & ".zip" Dim FTPUser As String = "user" Dim FTPPass As String = "pass" Dim FileInfo As New System.IO.FileInfo(FilePath) 'Create FtpWebRequest object from the Uri provided Dim FTPWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(UploadPath)), System.Net.FtpWebRequest) 'Provide the WebPermission Credintials FtpWebRequest.Credentials = New System.Net.NetworkCredential(FTPUser, FTPPass) 'Sets Proxy information FtpWebRequest.Proxy = Nothing 'By default KeepAlive is true, where the control connection is not closed after a command is executed. FtpWebRequest.KeepAlive = False 'Set timeout for 20 seconds FtpWebRequest.Timeout = 20000 'Specify the command to be executed. FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile 'Specify the data transfer type. FtpWebRequest.UseBinary = True 'Notify the server about the size of the uploaded file FtpWebRequest.ContentLength = FileInfo.Length 'The buffer size is set to 2kb Dim buffLength As Integer = 2048 Dim buff(buffLength - 1) As Byte 'Opens a file stream (System.IO.FileStream) to read the file to be uploaded Dim FileStream As System.IO.FileStream = FileInfo.OpenRead() Try 'Stream to which the file to be upload is written Dim Stream As System.IO.Stream = FtpWebRequest.GetRequestStream() 'Read from the file stream 2kb at a time Dim contentLen As Integer = FileStream.Read(buff, 0, buffLength) 'Till Stream content ends Do While contentLen <> 0 'Write Content from the file stream to the FTP Upload Stream Stream.Write(buff, 0, contentLen) contentLen = FileStream.Read(buff, 0, buffLength) Loop 'Close the file stream and the Request Stream Stream.Close() Stream.Dispose() FileStream.Close() FileStream.Dispose() Catch ex As Exception MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try
But this doesn't display any progress...
I have tried updating a ProgressBar I have placed, which would be useful as then I can customise the look of the progress, but even this doesn't move.
vb Code:
Private Sub bgw_UploadZip_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) ProgressBar1.Value = e.ProgressPercentage End Sub
Any help with this would be greatly appreciated.
I'm hoping to move towards upload folders and subfolders, from what I've read you do this by recursive scans through the local folders and create them on the remote server. If anyone has recomendation on FtpWebRequest or WebClient or something else for this, please let me know as I can start working with that now.
Thanks in advance.