1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| ' -------------------------------------------------------------------
' Fonction DownloadFile
'
' Arguments :
' [E] strUrl Adresse web fichier à télécharger
' [S] strFichierLocal Fichier téléchargé
' [S] strStatusText Statut http (texte) de l'opération
'
' Valeur renvoyée : Statut (Entier long) http
' -------------------------------------------------------------------
Private Function DownloadFile(strUrl As String, _
ByRef strFichierLocal As String, _
ByRef strStatusText As String) As Long
Dim wq As WinHttp.WinHttpRequest
Dim p As Long, lResult As Long
Dim ff As Integer, byArray() As Byte
' Extraire nom fichier de l'url
p = InStrRev(strUrl, "/") + 1
' Fichier pour sauvegarde en local
strFichierLocal = Environ("TEMP") & "\" & Mid(strUrl, p, Len(strUrl) + 1 - p)
' Nouvelle requête HTTP
Set wq = New WinHttpRequest
wq.Open "GET", strUrl
wq.Send
' Code et texte de retour de la requête http
lResult = wq.Status
strStatusText = wq.StatusText
' Si OK sauvegarder dans fichier local
If wq.Status = 200 Then
ff = FreeFile()
byArray() = wq.ResponseBody
If Len(Dir(strFichierLocal)) > 0 Then Kill strFichierLocal
' Ecrire dans fichier
Open strFichierLocal For Binary As ff
Put #ff, , byArray()
Close #ff
Set wq = Nothing
Else
strFichierLocal = ""
End If
DownloadFile = lResult
End Function |
Partager