Skip to content

Downloading Files

Dickson Law edited this page Apr 9, 2023 · 1 revision

Downloading Files

Script Functions

  • xhr_download(url, filename_or_buffer, options): Download from the given URL to the given file name or buffer ID.

The options Parameter

The options parameter is an untyped struct specifying additional properties for the download. Available keys are:

  • done: Specify a callback method that accepts a XhrDownloadResponse struct and performs actions on it when successful.
  • fail: Specify a callback method that accepts a XhrDownloadResponse struct 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%20World to 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 as noone, the daemon will not monitor anything else. Leave unspecified to use the default set by REQM_DEFALT_SUBJECT.
  • textMode: Specify whether to write the downloaded file's contents using file_text_write_string (true) or buffer_save_ext (false). On the HTML5 and OperaGX exports, this must be set to true if you wish to use the resulting emulated file with text file functions or INI file functions. Default: false

Classes

XhrDownloadResponse

The done and fail options take XhrDownloadResponse structs, which describe the outcome of the download.

Fields

  • 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 contain undefined.
  • handle: The asynchronous request ID returned by http_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 the sha1 option is given).
  • sha1Received: The SHA1 hash of the download (Available only when the sha1 option is given).

Methods

  • 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 the sha1 option was not given, this always returns true.

Clone this wiki locally