Credit goes to @Bruttosozialprodukt
Another post here
This could probably use some error handling and possibly some more parameters for customization, but it does work as is.
Eventually I plan to re-write the function to allow cancelling the download. As far as I'm aware Download() does not have an option to cancel/abort the download. Correct me if I'm wrong.
Code: Select all
DownloadWithProgress(downloadUrl, outputFile) {
_lastSize := 0
, _lastSizeTick := 0
, _url := downloadUrl
, _file := outputFile
, _fileSize := __GetNetworkFileSize()
, _Gui := Gui("+ToolWindow -SysMenu -DPIScale", _url)
, _speedSizeText := _Gui.AddText("xm ym w580 h20 Center", "Ready...")
, _progressBar := _Gui.AddProgress("xm y+m wp hp")
, _Gui.Show("AutoSize Center")
, SetTimer(__Update, 100)
, Download(_url, _file)
, SetTimer(__Update, 0)
, _Gui.Destroy()
return
__GetNetworkFileSize() {
wr := ComObject("WinHttp.WinHttpRequest.5.1")
, wr.open("HEAD", _url, false)
, wr.send()
, out := wr.getResponseHeader("Content-Length")
return out
}
__Update() {
if (!FileExist(_file))
return
currentSize := FileGetSize(_file)
, currentSizeTick := A_TickCount
, speed := Round((currentSize / 1024 - _lastSize / 1024) / ((currentSizeTick - _lastSizeTick) / 1000))
, _lastSizeTick := currentSizeTick
, _lastSize := currentSize
, percentDone := Round(currentSize / _fileSize * 100)
, _speedSizeText.Value := currentSize " of " _fileSize " bytes Downloaded @ " speed " Kb/s"
, _progressBar.Value := percentDone
, _Gui.Title := "Downloading " _url " (" percentDone "%)"
return
}
}