Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42334

VS 2010 FTP Upload, BackgroundWorker Progress Display

$
0
0
Hi there,

I've been successfully using a BackgroundWorker to display the upload progress of a single file with the following code:

vb Code:
  1. Private Sub bgw_UploadZip_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw_UploadZip.DoWork
  2.         'Uploads complete .zip file via FTP to client location
  3.         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:
  1. Private Sub bgw_UploadZip_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw_UploadZip.DoWork
  2.         'Uploads complete .zip file via FTP to client location
  3.         Dim FilePath As String = System.IO.Path.GetTempPath & ZipFilename & ".zip"
  4.         Dim UploadPath As String = "ftp://ftp.domain.com/" & ZipFilename & ".zip"
  5.         Dim FTPUser As String = "user"
  6.         Dim FTPPass As String = "pass"
  7.         Dim FileInfo As New System.IO.FileInfo(FilePath)
  8.  
  9.         'Create FtpWebRequest object from the Uri provided
  10.         Dim FTPWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(UploadPath)), System.Net.FtpWebRequest)
  11.  
  12.         'Provide the WebPermission Credintials
  13.         FtpWebRequest.Credentials = New System.Net.NetworkCredential(FTPUser, FTPPass)
  14.  
  15.         'Sets Proxy information
  16.         FtpWebRequest.Proxy = Nothing
  17.  
  18.         'By default KeepAlive is true, where the control connection is not closed after a command is executed.
  19.         FtpWebRequest.KeepAlive = False
  20.  
  21.         'Set timeout for 20 seconds
  22.         FtpWebRequest.Timeout = 20000
  23.  
  24.         'Specify the command to be executed.
  25.         FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
  26.  
  27.         'Specify the data transfer type.
  28.         FtpWebRequest.UseBinary = True
  29.  
  30.         'Notify the server about the size of the uploaded file
  31.         FtpWebRequest.ContentLength = FileInfo.Length
  32.  
  33.         'The buffer size is set to 2kb
  34.         Dim buffLength As Integer = 2048
  35.         Dim buff(buffLength - 1) As Byte
  36.  
  37.         'Opens a file stream (System.IO.FileStream) to read the file to be uploaded
  38.         Dim FileStream As System.IO.FileStream = FileInfo.OpenRead()
  39.  
  40.         Try
  41.             'Stream to which the file to be upload is written
  42.             Dim Stream As System.IO.Stream = FtpWebRequest.GetRequestStream()
  43.  
  44.             'Read from the file stream 2kb at a time
  45.             Dim contentLen As Integer = FileStream.Read(buff, 0, buffLength)
  46.  
  47.             'Till Stream content ends
  48.             Do While contentLen <> 0
  49.                 'Write Content from the file stream to the FTP Upload Stream
  50.                 Stream.Write(buff, 0, contentLen)
  51.                 contentLen = FileStream.Read(buff, 0, buffLength)
  52.             Loop
  53.  
  54.             'Close the file stream and the Request Stream
  55.             Stream.Close()
  56.             Stream.Dispose()
  57.             FileStream.Close()
  58.             FileStream.Dispose()
  59.         Catch ex As Exception
  60.             MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  61.         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:
  1. Private Sub bgw_UploadZip_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs)
  2.         ProgressBar1.Value = e.ProgressPercentage
  3.     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.

Viewing all articles
Browse latest Browse all 42334

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>