-
Notifications
You must be signed in to change notification settings - Fork 1
Downloading Files
Dickson Law edited this page Apr 9, 2023
·
1 revision
-
xhr_download(url, filename_or_buffer, options): Download from the given URL to the given file name or buffer ID.
The options parameter is an untyped struct specifying additional properties for the download. Available keys are:
-
done: Specify a callback method that accepts aXhrDownloadResponsestruct and performs actions on it when successful. -
fail: Specify a callback method that accepts aXhrDownloadResponsestruct and performs actions on it when the download is unsuccessful. -
headers: Specify a map ID or array with pairwise header-value entries, and add those to the download request's headers. For example, specifying[["Accept", "application/json"], ["Accept-Charset", "utf-8"]]here will ask the server to respond in UTF-8 JSON if able. -
params: If specified as an untyped struct, it will be URL-encoded and appended to the URL. For example, specifying{q: "Hello World"}here will append?q=Hello%20Worldto the URL. -
progress: Specify a callback method that accepts the current download progress and the total size of the download, and performs actions on it when the download is in progress. -
sha1: Specify the SHA1 hash to expect from the download. -
subject: Specify an instance ID to bind callback methods to. The daemon will also monitor its existence, and self-destruct if it no longer exists. If specified asnoone, the daemon will not monitor anything else. Leave unspecified to use the default set byREQM_DEFALT_SUBJECT. -
textMode: Specify whether to write the downloaded file's contents usingfile_text_write_string(true) orbuffer_save_ext(false). On the HTML5 and OperaGX exports, this must be set totrueif you wish to use the resulting emulated file with text file functions or INI file functions. Default:false
The done and fail options take XhrDownloadResponse structs, which describe the outcome of the download.
-
data: The buffer holding the full response. Notice: If the download targeted a file, this buffer will be freed after the callback is over. -
file: The file being downloaded to. If the download didn't target a file, this will containundefined. -
handle: The asynchronous request ID returned byhttp_request. -
headers: A map containing the headers of the response. -
httpStatus: The HTTP status code of the response (e.g. 200). -
url: The URL being requested. -
sha1Expected: The SHA1 hash expected of the download (Available only when thesha1option is given). -
sha1Received: The SHA1 hash of the download (Available only when thesha1option is given).
-
isSuccessful(): Return whether the download is considered successful (HTTP status is 2xx, SHA1 hash matches if given). -
isSha1Validated(): Return whether the SHA1 hash is matched. If thesha1option was not given, this always returns true.