【问题标题】:Super simple download with progress bar超级简单的下载进度条
【发布时间】:2015-04-05 16:26:29
【问题描述】:

在 AutoHotkey 中设置异步下载很麻烦,但如果您想在下载期间运行一些代码,例如更新进度条,这是必要的。

所以问题是: 有没有一种简单的方法来下载带有进度条的文件,而不包括庞大的 1000 多行库?

【问题讨论】:

    标签: download progress-bar autohotkey


    【解决方案1】:

    我很久以前就想出了那个代码,你仍然可以在 AHK 论坛上找到它,但是,为什么不与 Stackoverflow 社区分享它:

    DownloadFile(UrlToFile, SaveFileAs, Overwrite := True, UseProgressBar := True, ExpectedFileSize := 0) {
        ;Check if the file already exists and if we must not overwrite it
        If (!Overwrite && FileExist(SaveFileAs))
            Return
        ;Check if the user wants a progressbar
        If (UseProgressBar) {
            ;Initialize the WinHttpRequest Object
            WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
            ;Download the headers
            WebRequest.Open("HEAD", UrlToFile)
            WebRequest.Send()
    
            try {
                ;Store the header which holds the file size in a variable:
                FinalSize := WebRequest.GetResponseHeader("Content-Length")
            } catch e {
                ; Cannot get "Content-Length" header
                FinalSize := ExpectedFileSize
            }
    
            ;Create the progressbar and the timer
            Progress, , , Downloading..., %UrlToFile%
    
            LastSizeTick := 0
            LastSize := 0
    
            ; Enable progress bar updating if the system knows file size
            SetTimer, __UpdateProgressBar, 1500
        }
    
        ;Download the file
        UrlDownloadToFile, %UrlToFile%, %SaveFileAs%
        ;Remove the timer and the progressbar because the download has finished
        If (UseProgressBar) {
            Progress, Off
            SetTimer, __UpdateProgressBar, Off
        }
        Return
    
        ;The label that updates the progressbar
        __UpdateProgressBar:
            ;Get the current filesize and tick
            CurrentSize := FileOpen(SaveFileAs, "r").Length ;FileGetSize wouldn't return reliable results
            CurrentSizeTick := A_TickCount
    
            ;Calculate the downloadspeed
            SpeedOrig  := Round((CurrentSize/1024-LastSize/1024)/((CurrentSizeTick-LastSizeTick)/1000))
    
            SpeedUnit  := "KB/s"
            Speed      := SpeedOrig
    
            if (Speed > 1024) {
                ; Convert to megabytes
                SpeedUnit := "MB/s"
                Speed := Round(Speed/1024, 2)
            }
    
            SpeedText := Speed . " " . SpeedUnit
    
            ;Save the current filesize and tick for the next time
            LastSizeTick := CurrentSizeTick
            LastSize := FileOpen(SaveFileAs, "r").Length
    
            if FinalSize = 0
            {
                PercentDone := 50
            } else {
                ;Calculate percent done
                PercentDone := Round(CurrentSize/FinalSize*100)
                SpeedText := SpeedText . ", " . Round((FinalSize - CurrentSize) / SpeedOrig / 1024) . "s left"
            }
    
            ;Update the ProgressBar
            Progress, %PercentDone%, %PercentDone%`% (%SpeedText%), Downloading..., Downloading %SaveFileAs% (%PercentDone%`%)
        Return
    }
    

    这里有一些例子:

    示例 1 - 下载带有进度条的 firefox 设置,如果磁盘上已经存在,则将其覆盖:

    DownloadFile("http://download-installer.cdn.mozilla.net/pub/firefox/releases/26.0/win32/en-US/Firefox%20Setup%2026.0.exe", "firefox_setup.exe")
    

    示例 2 - 下载带有进度条的 Autohotkey,如果已存在则不要覆盖它:

    Url = http://ahkscript.org/download/ahk-install.exe
    DownloadAs = AutoHotkey_L Installer.exe
    Overwrite := False
    UseProgressBar := True
    DownloadFile(Url, DownloadAs, Overwrite, UseProgressBar)
    

    示例 3 - 下载 CCleaner 设置并打开“另存为”对话框,询问用户文件的保存位置,如果已存在则覆盖它:

    FileSelectFile, SaveAs, S, ccsetup410.exe
    DownloadFile("http://download.piriform.com/ccsetup410.exe", SaveAs, True, True)
    

    【讨论】:

    • 更多关于代码的讨论在AHK forum
    • 嗨@Forivin!感谢您提供此代码!看起来挺好的。你知道,我收到了一个错误:the requested header not found 表示FinalSize := WebRequest.GetResponseHeader("Content-Length") 行。任何想法如何处理它?
    • 如果没有内容长度头,那么就不会有正文(文件)。您确定您尝试的链接仍然有效吗?
    • 是的,它正在工作。但你知道,有时服务器不提供文件大小。
    • 刚刚添加了一些改进。请检查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多