This is just meant as an example to learn from!
Feel free to improve it or fix any bugs!

Code: Select all
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Downloader with progress and status;;
;;By Joakim L. Christiansen ;;
;;Version 1.1 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EnableExplicit
Global DownloadURL$, DownloadFilename$, StopDownload, IsDownloading
#Main = 0 ;Window
Enumeration ;Gadgets
#Text
#Download
#URL
#Progress
EndEnumeration
Procedure DownloadFile(Dummy)
Protected hInet, hURL, Bytes
Protected BufferLength = 2048, Buffer$ = Space(BufferLength)
Protected Url$ = DownloadURL$
Protected Filename$ = DownloadFilename$
Protected File
Protected CurrentSize, PreviousSize, FileSize, Time, BytesPerSecond
Protected Domain$, String$, i, BufferLengthWas = BufferLength
Protected hInetCon, hHttpOpenRequest, iretval
hInet = InternetOpen_("Downloader",0,0,0,0)
hURL = InternetOpenUrl_(hInet,Url$,0,0,$80000000,0)
;Get filesize
Domain$ = StringField(Url$,3,"/")
hInetCon = InternetConnect_(hInet,Domain$,80,#Null,#Null,3,0,0)
If hInetCon
hHttpOpenRequest = HttpOpenRequest_(hInetCon,"HEAD",ReplaceString(Url$,"http://"+Domain$+"/",""),#Null,#Null,0,$80000000,0)
If hHttpOpenRequest
iretval = HttpSendRequest_(hHttpOpenRequest,#Null,0,0,0)
If iretval
HttpQueryInfo_(hHttpOpenRequest,19,@Buffer$,@BufferLength,0) ;changes the buffer length
String$ = PeekS(@Buffer$,BufferLength): BufferLength = BufferLengthWas
If Trim(String$) = "200"
HttpQueryInfo_(hHttpOpenRequest,22,@Buffer$,@BufferLength,0)
String$ = PeekS(@Buffer$,BufferLength): BufferLength = BufferLengthWas
If FindString(String$,"Content-Length:",1)
i = FindString(String$,"Content-Length:",1) + Len("Content-Length:")
String$ = Mid(String$,i,Len(String$)-i)
FileSize = Val(Trim(String$))
EndIf
EndIf
EndIf
EndIf
EndIf
;Download file and update status
If hURL
File = CreateFile(#PB_Any,Filename$)
If File
Time = ElapsedMilliseconds()
SetGadgetAttribute(#Progress,#PB_ProgressBar_Maximum,FileSize)
While InternetReadFile_(hURL,@Buffer$,BufferLength,@Bytes) And Bytes > 0
If StopDownload
Break
EndIf
WriteData(File,@Buffer$,Bytes)
;File + PeekS(@Buffer,Bytes)
CurrentSize + Bytes
SetGadgetState(#Progress,CurrentSize)
SetGadgetText(#Text,Str(CurrentSize/1024)+"/"+Str(FileSize/1024)+"kb - "+Str(BytesPerSecond/1024)+"kb/s")
If Time < ElapsedMilliseconds() - 1000
Time = ElapsedMilliseconds()
BytesPerSecond = CurrentSize - PreviousSize
PreviousSize = CurrentSize
EndIf
Wend
CloseFile(File)
Else
MessageRequester("Warning!","Error creating file!",#MB_ICONWARNING)
EndIf
;Reset status
StopDownload = #False
IsDownloading = #False
SetGadgetText(#Text,"0/0kb - 0kb/s")
SetGadgetState(#Progress,0)
SetGadgetText(#Download,"Download")
;If file is incomplete then delete it
If CurrentSize < FileSize
DeleteFile(Filename$)
Else
MessageRequester("Info","Download complete!",#MB_ICONINFORMATION)
EndIf
Else
MessageRequester("Warning!","Download failed!",#MB_ICONWARNING)
EndIf
InternetCloseHandle_(hURL)
InternetCloseHandle_(hInetCon)
InternetCloseHandle_(hInet)
EndProcedure
If OpenWindow(#Main,0,0,300,70,"Downloader with progress and status",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) And CreateGadgetList(WindowID(#Main))
StringGadget(#URL,5,5,225,20,"http://www.mannindustries.net/soldat/soldat131.zip"): ButtonGadget(#Download,235,5,60,20,"Download")
ProgressBarGadget(#Progress,5,30,290,20,0,100)
TextGadget(#Text,5,54,290,20,"0/0kb - 0kb/s",#PB_Text_Center)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
StopDownload = #True
Delay(20)
Break
Case #PB_Event_Gadget
Select EventGadget()
Case #Download
If Not IsDownloading
DownloadURL$ = GetGadgetText(#URL)
DownloadFilename$ = SaveFileRequester("Save as",StringField(DownloadURL$,CountString(DownloadURL$,"/")+1,"/"),"*.*",0)
If DownloadURL$ And DownloadFilename$
CreateThread(@DownloadFile(),0)
SetGadgetText(#Download,"Stop")
IsDownloading = #True
EndIf
Else
StopDownload = #True
IsDownloading = #False
SetGadgetState(#Progress,0)
SetGadgetText(#Download,"Download")
EndIf
EndSelect
EndSelect
ForEver
Else
MessageRequester("Warning!","Error opening window!",#MB_ICONWARNING)
EndIf