show download progress

  • Thread starter Thread starter Loane Sharp
  • Start date Start date
L

Loane Sharp

Hi there

I'm currently using WebClient.DownloadFile to download a file from the
server to my local disk. Is there a way to show the progress of the file
download?

Best regards
Loane
 
Hi H

Thank you for pointing me in the right direction. I have one small question
.... Why do I get the following error message from the JIT debugger? "An
exception 'System.IO.IOException' has occurred in synchronize.exe".

My code (a bit abbreviated) is as follows ...

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO
Imports System.Net

Public Class synchronize

Inherits System.Windows.Forms.Form

Shared frmSync As Form
Shared WithEvents btn As Button
Shared lblInfo As Label
Shared pbrSync As ProgressBar
Shared res_App, res_Dat, res_Cht, cdr_Fisheye, cdr_FisheyeTemp,
emp_FisheyeTemp As Integer
Shared len_App, len_Dat, len_Cht As Double
Shared datBuffer() As Byte
Const pktSize As Integer = 65536
Shared m_resp As HttpWebResponse
Shared m_fs As FileStream

Shared Sub Main()
Application.Run(New synchronize())
End Sub

Public Sub New()
frmSync = New Form()

[some other code skipped]

m_fs = New FileStream("C:\Fisheye\temp\appupdate.zip",
FileMode.Create)
m_resp.GetResponseStream().BeginRead(datBuffer, 0, pktSize, New
AsyncCallback(AddressOf On_datRead), frmSync)

[some more code skipped]

End Sub

Shared Sub On_datRead(ByVal res As IAsyncResult)
Dim pktBytes As Integer = m_resp.GetResponseStream().EndRead(res)
m_fs.Write(datBuffer, 0, pktBytes)
pbrSync.Value = pbrSync.Value + pktBytes
Application.DoEvents()
If pktBytes > 0 Then
m_resp.GetResponseStream().BeginRead(datBuffer, 0, pktSize,
New AsyncCallback(AddressOf On_datRead), frmSync)
Else:
m_fs.Close()
m_fs = Nothing
End If
End Sub

End Class
 
Herfried,

I had the idea that it was impossible to use the progressbar with the
webclient.downloadfile

However you gave a link, I did not check all 1000 other links inside that.

Can you give the exact link where that is written because I am very curious
about that.

Cor
 
Hi H

The CLR debugger gives the following additional information on the
IOException ...

"Additional information: The process cannot access the file "appupdate.zip"
because it is being used by another process."

Hopefully this helps

Best regards
Loane
 
Hi,

Here is a quick example. Add these import statements.

Imports System.Net

Imports System.IO



Sample code



Dim request As WebRequest

Dim response As WebResponse

Dim reader As Stream

Dim writer As Stream

Dim data(1023) As Byte

Dim count As Integer

Dim total As Integer

Me.Show()

Me.Text = "Downloading file ....."

Application.DoEvents()

request =
WebRequest.Create("http://www.onteorasoftware.com/downloads/multigrids.zip")

response = request.GetResponse()

reader = response.GetResponseStream()

ProgressBar1.Maximum = CInt(response.ContentLength)

ProgressBar1.Value = 0

total = 0

writer = File.Create("mylocaldata.zip")

While True

count = reader.Read(data, 0, 1024)

If count <= 0 Then

Exit While

End If

writer.Write(data, 0, count)

total += 1024

If total < ProgressBar1.Maximum Then ProgressBar1.Value = total

Application.DoEvents()

End While

reader.Close()

writer.Close()



Ken

--------------------------


Hi there

I'm currently using WebClient.DownloadFile to download a file from the
server to my local disk. Is there a way to show the progress of the file
download?

Best regards
Loane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top