Download

Downloads a file from the Internet.

Download URL, Filename 

Parameters

URL

Type: String

URL of the file to download. For example, "https://someorg.org" might retrieve the welcome page for that organization.

Filename

Type: String

Specify the name of the file to be created locally, which is assumed to be in A_WorkingDir if an absolute path isn't specified. Any existing file will be overwritten by the new file.

This function downloads to a file. To download to a variable instead, see the example below.

Error Handling

A transport failure and a refused or unserved FTP request each throw an OSError. A URL which is not absolute, a scheme other than http, https and ftp, and a cache flag other than *0 each throw a ValueError.

Remarks

The download might appear to succeed even when the remote file doesn't exist. This is because many web servers send an error page instead of the missing file. This error page is what will be saved in place of Filename.

Firewalls or the presence of multiple network adapters may cause this function to fail. Also, some websites may block such downloads.

Caching: By default, the URL is retrieved directly from the remote server. To permit caching, precede the URL with *0 followed by a space; for example: "*0 https://someorg.org".

Proxies: For an http or https URL the system proxy configuration is used. To name a proxy, or to send credentials or request headers, use the Download method of an Http session.

FTP URLs are also supported, using passive-mode binary transfers. For example:

Download "ftp://example.com/home/My File.zip", "C:\My Folder\My File.zip"  ; Log in anonymously.
Download "ftp://user:pass@example.com:21/home/My File.zip", "C:\My Folder\My File.zip"  ; Log in as a specific user.
Download "ftp://user:pass@example.com/My Directory", "C:\Dir Listing.txt"  ; Gets a directory listing.

A directory URL saves the server's plain-text LIST output; AutoHotkey saves an HTML listing.

Http, FileRead, FileCopy

Examples

Downloads a text file.

Download "https://www.autohotkey.com/download/2.0/version.txt", "C:\AutoHotkey Latest Version.txt"

Downloads a zip file.

Download "https://someorg.org/archive.zip", "C:\SomeOrg's Archive.zip"

Downloads text to a variable.

whr := ComObject("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "https://www.autohotkey.com/download/2.0/version.txt", true)
whr.Send()
; Using 'true' above and the call below allows the script to remain responsive.
whr.WaitForResponse()
version := whr.ResponseText
MsgBox version

Makes an asynchronous HTTP request.

req := ComObject("Msxml2.XMLHTTP")
; Open a request with async enabled.
req.open("GET", "https://www.autohotkey.com/download/2.0/version.txt", true)
; Set our callback function.
req.onreadystatechange := Ready
; Send the request.  Ready() will be called when it's complete.
req.send()
/*
; If you're going to wait, there's no need for onreadystatechange.
; Setting async=true and waiting like this allows the script to remain
; responsive while the download is taking place, whereas async=false
; will make the script unresponsive.
while req.readyState != 4
    sleep 100
*/
Persistent

Ready() {
    if (req.readyState != 4)  ; Not done yet.
        return
    if (req.status == 200) ; OK.
        MsgBox "Latest AutoHotkey version: " req.responseText
    else
        MsgBox "Status " req.status,, 16
    ExitApp
}