Home Page
Articoli
Tips & Tricks
News
Forum
Archivio Forum
Blogs
Sondaggi
Rss
Video
Utenti
Chi Siamo
Contattaci
Username:
Password:
Login
Registrati ora!
Recupera Password
Home Page
Stanze Forum
App. WinForms / WPF .NET
WebClient - DownloadFileAsync - Progressbar
martedì 24 marzo 2009 - 16.58
Elenco Threads
Stanze Forum
Aggiungi ai Preferiti
Cerca nel forum
alisandre
Profilo
| Newbie
14
messaggi | Data Invio:
mar 24 mar 2009 - 16:58
Ciao a tutti. Non riesco a ricavare in nessun modo la dimensione del file che devo scaricare. Ho seguito una guida msdn ed ho fatto l'ovverride della funzione GetWebRequest impostando la modalità attiva, ma ottengo sempre la percentuale a zero. Qualcuno sa come risolvere il problema? Grazie mille. Alessandra.
Questo è il codice che uso per effettuare l'ovverride.
Private Class ActiveWebClient
Inherits Net.WebClient
Protected Overrides Function GetWebRequest(ByVal Address As Uri) As System.Net.WebRequest
Dim req As System.Net.FtpWebRequest
req = MyBase.GetWebRequest(Address)
req.UsePassive = False
Return req
End Function
End Class
Questo invece è il codice utilizzato per effettuare il download del file:
wcAggiornamenti = New ActiveWebClient
wcAggiornamenti.Credentials = New NetworkCredential(sUserFTP, sPasswordFTP)
wpProxy = New System.Net.WebProxy
wpProxy.UseDefaultCredentials = True
wcAggiornamenti.Proxy = wpProxy
DownloadFile:
bUpDownErrore = False
bUpDownCompletato = False
wcAggiornamenti.DownloadFileAsync(New Uri(source), target)
'variabili che indicano la presenza di errori vengono gestite in wcAggiornamenti_DownloadFileCompleted
While Not (bUpDownCompletato Or bUpDownErrore)
Application.DoEvents()
End While
If bUpDownErrore = True Then
iTentativi += 1
If iTentativi <= 3 Then GoTo DownloadFile
ElseIf bUpDownCompletato = True Then
AggiungiLog("Download di " & description & " completato.")
End If
luxsor
Profilo
| Guru
1.079
messaggi | Data Invio:
mar 24 mar 2009 - 20:50
Ciao Alessandra,
prova ad imprementare il DownloadProgressCallBack che tramite e.ProgressPercentage ti dovrebbe restituire un valore in percentuale, quindi non ti serve più conoscere la dimensione del file.
Imposta il ProgressBar.Max=100
Private Shared Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
Console.WriteLine("0} downloaded 1} of 2} bytes. 3} % complete...", _
CStr(e.UserState), e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage)
End Sub
Fammi sapere
alisandre
Profilo
| Newbie
14
messaggi | Data Invio:
mer 25 mar 2009 - 10:48
Ciao. Non so se è lo stesso ma io ho già una sub che gestisce il progress (ho dimenticato di postarlo), ma non da mai la percentuale intermedia. Passa da 0 a 100.
Private Sub wcAggiornamenti_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) _
Handles wcAggiornamenti.DownloadProgressChanged
Dim sByteRic As String
iUpDownByte = e.BytesReceived
sByteRic = Format(iUpDownByte / 1048576, "#0.00")
slDownload.Text = sByteRic & " MB"
Refresh()
End Sub
Non è la stessa cosa usare questo o usare il downloadcallback che tu mi hai suggerito? Grazie mille ancora. Alessandra.
alisandre
Profilo
| Newbie
14
messaggi | Data Invio:
mer 25 mar 2009 - 11:31
Ho fatto alcune prove. In questo momento i file vengono scaricati via FTP (FTP://ftp.dominio.it/dominio.it/file.ext)
se anzichè via FTP li scarico via HTTP (eliminando l'override della getwebrequest e le credenziali dell'ftp) la percentuale viene visualizzata correttamente (
http://www.dominio.it/file.ext
): Purtroppo devo per forza farlo via FTP. Quale potrebbe essere il problema? Grazie. Alessandra.
luxsor
Profilo
| Guru
1.079
messaggi | Data Invio:
mer 25 mar 2009 - 12:45
>Private Sub wcAggiornamenti_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) _
>Handles wcAggiornamenti.DownloadProgressChanged
>Dim sByteRic As String
>iUpDownByte = e.BytesReceived
>sByteRic = Format(iUpDownByte / 1048576, "#0.00")
>slDownload.Text = sByteRic & " MB"
>Refresh()
>End Sub
Ma nella funzione stai usando e.ByteReceived?
Hai inserito tale funzione un un thread?
Facci sapere
alisandre
Profilo
| Newbie
14
messaggi | Data Invio:
mer 25 mar 2009 - 13:01
Ecco la Sub come era in origine, prima che scoprissi che non funzionava, usava BytesReceived, TotalBytesToReceive e e.ProgressPercentage
Private Sub wcAggiornamenti_DownloadProgressChanged(ByVal sender As Object, _
ByVal e As System.Net.DownloadProgressChangedEventArgs) _
Handles wcAggiornamenti.DownloadProgressChanged
Dim sByteRic As String
Dim sByteTot As String
Dim iPerc As Integer
iUpDownByte = e.BytesReceived
iUpDownByteTotali = e.TotalBytesToReceive
iUpDownPercentuale = e.ProgressPercentage
sByteRic = Format(iUpDownByte / 1048576, "#0.00")
sByteRic += 0.01
sByteTot = Format(iUpDownByteTotali / (1024 * 1024), "#0.00")
sByteTot += 0.01
iPerc = (iUpDownByte * 100 / iUpDownByteTotali)
slDownload.Text = sByteRic & " MB di " & sByteTot & " MB"
pbProgress.Value = iUpDownPercentuale
End Sub
Scusa la mia ignoranza, ma che intendi per inserire la funzione in un thread?
luxsor
Profilo
| Guru
1.079
messaggi | Data Invio:
mer 25 mar 2009 - 14:32
>Private Sub wcAggiornamenti_DownloadProgressChanged(ByVal sender As Object, _
>ByVal e As System.Net.DownloadProgressChangedEventArgs) _
>Handles wcAggiornamenti.DownloadProgressChanged
>Dim sByteRic As String
>Dim sByteTot As String
>Dim iPerc As Integer
>iUpDownByte = e.BytesReceived
>iUpDownByteTotali = e.TotalBytesToReceive
>iUpDownPercentuale = e.ProgressPercentage
>sByteRic = Format(iUpDownByte / 1048576, "#0.00")
>sByteRic += 0.01
>sByteTot = Format(iUpDownByteTotali / (1024 * 1024), "#0.00")
>sByteTot += 0.01
>iPerc = (iUpDownByte * 100 / iUpDownByteTotali)
>slDownload.Text = sByteRic & " MB di " & sByteTot & " MB"
>pbProgress.Value = iUpDownPercentuale
come la dichiare la variabile iUpDownPercentuale?
perchè non li passi direttamente
pbProcess.Value=e.ProgressPercentage
Dai un occhiata a questo esempio completo
http://www.codeproject.com/KB/vb/downloadFileswProgressbar.aspx
Facci sapere
alisandre
Profilo
| Newbie
14
messaggi | Data Invio:
mer 25 mar 2009 - 15:05
Arcano mistero risolto! E' il server FTP che non manda il la risposta. Ho provato in un altro server FTP e tutto funziona a meraviglia. Grazie mille cmq per l'aiuto! Alessandra.
luxsor
Profilo
| Guru
1.079
messaggi | Data Invio:
mer 25 mar 2009 - 15:16
>Grazie mille cmq per l'aiuto! Alessandra.
Di Nulla
Torna su
Stanze Forum
Elenco Threads
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?
Dopo esserti registrato potrai chiedere
aiuto sul nostro
Forum
oppure aiutare gli altri
Consulta le
Stanze
disponibili.
Registrati ora !