ho fatto una classe downloadFile che serve proprio a questo
codice:
Option Explicit
'-----------------------------------------------------------------------------
'Class : downloadFile
'Date : 10/09/2004
'Author : Fabio Pileri
'Purpose : Download and Save File from Internet with progress bar
'References : iNet (Microsoft Internet Transfer Control)
' ProgressBar (Microsoft Windows Common Controls 6.0 - SP4)
'Notes :
' Dim dl As DownloadFile ' istanza della classe
' Set dl = New DownloadFile
' Set dl.setInet = me.Inet1 ' Imposta il valore di una proprietà dell'oggetto Inet
' Set dl.setPrgPercentDone = Me.prgPercentDone ' Imposta il valore di una proprietà dell'oggetto ProgressBar
' dl.DownloadFile Me.txtSourceFile.Text, Me.txtDestFile.Text ' richiamo evento
' dl.getDownloadingFile ' (proprietà tipo boolean) il downloading è stato effettuato
' dl.getLocalSaveFile ' (proprietà tipo string) il percorso e nome del file salvato
'-----------------------------------------------------------------------------
Private m_GettingFileSize As Boolean
Private m_DownloadingFile As Boolean
Private m_DownloadingFileSize As Long
Private m_LocalSaveFile As String
Private prgPercentDone As ProgressBar
Private WithEvents inetDownloadFile As Inet
'IL FILE E' STATO SCARICATO
Private b_DownloadingFile As Boolean
Private Sub NoInternetConnection(ResponseCode As String, ResponseInfo As String)
On Error Resume Next
Select Case ResponseCode
Case 12007: MsgBox "Non sei connesso ad Internet, oppure il server è temporaneamente offline." & vbCrLf & _
"Per favore controlla le impostazioni per l'accesso ad Internet e riprova.", vbOKOnly + vbCritical, "Controlla il tuo collegamento ad Internet"
Case 35761: MsgBox "Errore durante il trasferimento dei file." & vbCrLf & _
"Controlla il tuo collegamento ad Internet e riprova.", vbOKOnly + vbCritical, "Problemi durante il downloading dei file"
Case Else: MsgBox "There has been occured a problem while downloading the bizzit files." & vbCrLf & _
"Controlla il tuo collegamento ad Internet e riprova.", vbOKOnly + vbCritical, "Problemi durante il downloading dei file"
End Select
End Sub
Public Sub DownloadFile(strSrcHTTPFile As String, strDestSaveFile As String)
m_DownloadingFile = False
m_DownloadingFileSize = GetHTTPFileSize(strSrcHTTPFile)
If m_DownloadingFileSize <> -1 Then
m_LocalSaveFile = strDestSaveFile
m_DownloadingFile = True
inetDownloadFile.Execute strSrcHTTPFile, "GET " & Chr(34) & strDestSaveFile & Chr(34)
Else
MsgBox "Errore sulle informazione del file", vbCritical, "Problemi durante il downloading dei file"
End If
End Sub
Private Function GetHTTPFileSize(strHTTPFile As String) As Long
On Error GoTo ErrorHandler
Dim GetValue As String
Dim GetSize As Long
m_GettingFileSize = True
inetDownloadFile.Execute strHTTPFile, "HEAD " & Chr(34) & strHTTPFile & Chr(34)
Do Until inetDownloadFile.StillExecuting = False
DoEvents
Loop
GetValue = inetDownloadFile.GetHeader("Content-length")
Do Until inetDownloadFile.StillExecuting = False
DoEvents
Loop
If IsNumeric(GetValue) = True Then
GetSize = CLng(GetValue)
Else
GetSize = -1
End If
If GetSize <= 0 Then GetSize = -1
m_GettingFileSize = False
GetHTTPFileSize = GetSize
Exit Function
ErrorHandler:
m_GettingFileSize = False
GetHTTPFileSize = -1
End Function
Private Sub inetDownloadFile_StateChanged(ByVal State As Integer)
On Error Resume Next
Dim vtData() As Byte
Dim FreeNr As Integer
Dim SizeDone As Long
Dim bDone As Boolean
Dim GetPerc As Integer
Select Case State ' ... Other cases not shown.
Case icError: ' 11
If inetDownloadFile.ResponseCode = 12007 Then
NoInternetConnection inetDownloadFile.ResponseCode, inetDownloadFile.ResponseInfo
End If
Case icResponseCompleted: ' 12
If m_GettingFileSize = True Then
Exit Sub
End If
If m_DownloadingFile = True Then
Kill m_LocalSaveFile
FreeNr = FreeFile
Open m_LocalSaveFile For Binary Access Write As FreeNr
Do While Not bDone
vtData = inetDownloadFile.GetChunk(1024, icByteArray)
SizeDone = SizeDone + UBound(vtData)
GetPerc = (SizeDone / m_DownloadingFileSize) * 100
If GetPerc > 100 Then GetPerc = 100
If GetPerc < 0 Then GetPerc = 0
prgPercentDone.Value = GetPerc
Put #FreeNr, , vtData()
If UBound(vtData) = -1 Then
bDone = True
Else
DoEvents
End If
Loop
Close FreeNr
b_DownloadingFile = True
MsgBox "File scaricato", vbInformation
prgPercentDone.Value = 0
End If
End Select
End Sub
Private Sub Class_Initialize()
m_GettingFileSize = False
m_DownloadingFile = False
b_DownloadingFile = False
End Sub
Private Sub Class_Terminate()
Set inetDownloadFile = Nothing
Set prgPercentDone = Nothing
End Sub
Public Property Set setInet(ByRef cboData As Inet)
Set inetDownloadFile = cboData
End Property
Public Property Set setPrgPercentDone(ByRef cboData As ProgressBar)
Set prgPercentDone = cboData
End Property
Public Property Get getLocalSaveFile()
getLocalSaveFile = m_LocalSaveFile
End Property
Public Property Get getDownloadingFile()
getDownloadingFile = b_DownloadingFile
End Property