dobro a raison c'est mieux avec le code source.
j'ai un peu regardé, c'est pas mal déjà ce que tu as fait.
j'ai corrigé deux trois trucs et ajouté deux trois MessageRequester / SetWindowTitle pour voir ce qui se passe.
à priori tout fonctionne bien.
quand tu dis que çà bloque, en fait çà bloque pas, il faut attendre un peu, qq secondes. chez moi dès fois j'attends 5 à 10 secondes (au niveau du premier appel à InternetReadFile) mais çà fini toujours par démarrer...
Code : Tout sélectionner
Import "shlwapi.lib"
StrFormatKBSizeA(Size.q, *BufStr, BufSize.l)
StrFormatByteSize64A(Size.q, *BufStr, BufSize.l)
EndImport
Procedure.s FormatKB(Size.q)
Protected BufStr.s{64}
If StrFormatKBSizeA(Size, @BufStr, 64)
ProcedureReturn PeekS(@BufStr, -1, #PB_Ascii)
EndIf
EndProcedure
Procedure.s FormatSize(Size.q)
Protected BufStr.s{64}
If StrFormatByteSize64A(Size, @BufStr, 64)
ProcedureReturn PeekS(@BufStr, -1, #PB_Ascii)
EndIf
EndProcedure
Enumeration
#Window_0
#pourcentage
#ProgressBar_0
#Boutton_0
EndEnumeration
Global Quit.b
Global Close.b
Procedure update()
DownloadURL.s = "http://www.purebasic.com/download/PureBasic_Demo.exe"
DownloadFilename.s= "PureBasic_Demo.exe"
Protected hInet, hURL, Bytes
Protected BufferLength = 2048, Buffer.s = Space(BufferLength)
Protected Url.s = DownloadURL.s
Protected Filename.s = DownloadFilename.s
Protected File
Protected CurrentSize, PreviousSize, FileSize, time, BytesPerSecond
Protected Domain.s, String.s, i, BufferLengthWas = BufferLength
Protected hInetCon, hHttpOpenRequest, iretval
hInet = InternetOpen_("Downloader",0,0,0,0)
hURL = InternetOpenUrl_(hInet,Url.s,0,0,$80000000,0)
;Get filesize
Domain.s = StringField(Url.s,3,"/")
SetWindowTitle(0, "InternetConnect_")
hInetCon = InternetConnect_(hInet,Domain.s,80,#Null,#Null,3,0,0)
If hInetCon
SetWindowTitle(0, "HttpOpenRequest")
hHttpOpenRequest = HttpOpenRequest_(hInetCon,"HEAD",ReplaceString(Url.s,"http://"+Domain.s+"/",""),#Null,#Null,0,$80000000,0)
If hHttpOpenRequest
SetWindowTitle(0, "HttpSendRequest")
iretval = HttpSendRequest_(hHttpOpenRequest,#Null,0,0,0)
If iretval
SetWindowTitle(0, "HttpQueryInfo")
HttpQueryInfo_(hHttpOpenRequest,19,@Buffer.s,@BufferLength,0) ;changes the buffer length
String.s = PeekS(@Buffer.s,BufferLength): BufferLength = BufferLengthWas
If Trim(String.s) = "200"
HttpQueryInfo_(hHttpOpenRequest,22,@Buffer.s,@BufferLength,0)
String.s = PeekS(@Buffer.s,BufferLength): BufferLength = BufferLengthWas
;MessageRequester("debug", string)
If FindString(String.s,"Content-Length:",1)
i = FindString(String.s,"Content-Length:",1) + Len("Content-Length:")
String.s = Trim(Mid(String.s,i))
FileSize = Val(String.s)
EndIf
EndIf
EndIf
EndIf
EndIf
;Download file and update status
If hURL
SetWindowTitle(0, "FileSize")
taille_fichier = FileSize("PureBasic_Demo.exe")
If taille_fichier > 0 ;le fichier existe déjà alors reprise
CurrentSize = taille_fichier
If CurrentSize = FileSize
MessageRequester("Information", "Le fichier existe déjà et semble complet !", #MB_ICONINFORMATION)
Quit = #True
Else
message.s
message = "Le fichier existe déjà !" + Chr(10)
message + "Taille du fichier à reprendre: " + FormatSize(CurrentSize) + Chr(10)
message + "Taille du fichier à télécharger: " + FormatSize(FileSize) + Chr(10) + Chr(10)
message + "Oui pour reprendre, Non pour télécharger à nouveau, ou Annuler."
Select MessageRequester("Information", Message, #PB_MessageRequester_YesNoCancel | #MB_ICONQUESTION)
Case #IDYES
File = OpenFile(#PB_Any, "PureBasic_Demo.exe") ;ouverture du fichier
If File
SetWindowTitle(0, "FileSeek: " + FormatSize(CurrentSize))
FileSeek(File, CurrentSize) ;on place le pointeur d'écriture là où on s'était arreté
SetWindowTitle(0, "InternetSetFilePointer: " + FormatSize(CurrentSize))
If InternetSetFilePointer_(hURL, CurrentSize, 0, #FILE_BEGIN ,0) <> CurrentSize
MessageRequester("Erreur", "Echec de InternetSetFilePointer_()", #MB_ICONERROR)
EndIf
EndIf
Case #IDNO
CurrentSize = 0
Case #IDCANCEL
Quit = #True
EndSelect
EndIf
EndIf
If Not File
File = CreateFile(#PB_Any,Filename.s)
EndIf
If File
time = ElapsedMilliseconds()
SetWindowTitle(0, "InternetReadFile")
DisableGadget(#Boutton_0, #False)
While ( Quit = #False ) And ( InternetReadFile_(hURL,@Buffer.s,BufferLength,@Bytes) = #True ) And ( Bytes > 0 )
CurrentSize + WriteData(File,@Buffer.s,Bytes)
SetWindowTitle(0, "Téléchargement: " + FormatSize(CurrentSize) + " / " + FormatSize(FileSize))
Pourcentage.f = (CurrentSize/FileSize)*100
SetGadgetState(#ProgressBar_0, Pourcentage)
If time < ElapsedMilliseconds() - 1000
time = ElapsedMilliseconds()
BytesPerSecond = CurrentSize - PreviousSize
PreviousSize = CurrentSize
EndIf
Wend
DisableGadget(#Boutton_0, #True)
CloseFile(File)
EndIf
EndIf
SetWindowTitle(0, "InternetCloseHandle: hUrl")
InternetCloseHandle_(hURL)
SetWindowTitle(0, "InternetCloseHandle: hInetCon")
InternetCloseHandle_(hInetCon)
SetWindowTitle(0, "InternetCloseHandle: hInet")
InternetCloseHandle_(hInet)
Close = #True
EndProcedure
If OpenWindow(#Window_0, 0, 0, 250, 105, "Veuillez patienter...", #PB_Window_ScreenCentered)
If CreateGadgetList(WindowID(#Window_0))
ProgressBarGadget(#ProgressBar_0, 21, 30, 207, 20, 0, 100)
ButtonGadget(#Boutton_0, 95, 77, 60, 20, "Annuler")
DisableGadget(#Boutton_0, #True)
EndIf
EndIf
CreateThread(@update(), 0)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget ;si on est dans un gadget
Select EventGadget()
Case #Boutton_0
SetWindowTitle(0, "Annulation...")
DisableGadget(#Boutton_0, #True)
Quit = #True
EndSelect
EndSelect
Until Close = #True
End