downloads.download()
The download()
function of the downloads
API downloads a file, given its URL and other optional preferences.
If the URL uses the HTTP or HTTPS protocol, the request includes all the relevant cookies, that is, those cookies set for the URL's hostname, secure flag, path, and so on. The default cookies, the cookies from the normal browsing session, are used unless:
- the
incognito
option is used, then the private browsing cookies are used.
- the
cookieStoreId
option is used, then the cookies from the specified store are used.
If both filename
and saveAs
are specified, the Save As dialog is displayed, populated with the filename
.
This is an asynchronous function that returns a Promise
.
Syntax
let downloading = browser.downloads.download(
options
)
Parameters
-
options
-
An object
specifying what file you wish to download, and any other preferences you wish to set concerning the download. It can contain the following properties:
allowHttpErrors
Optional
-
A boolean
flag that enables downloads to continue even if they encounter HTTP errors. Using this flag, for example, enables the download of server error pages. Default value false
. When set to:
false
, the download is canceled when it encounters an HTTP error.
true
, the download continues when an HTTP error is encountered and the HTTP server error is not reported. However, if the download fails due to file-related, network-related, user-related, or other error, that error is reported.
body
Optional
-
A string
representing the post body of the request.
conflictAction
Optional
-
A string representing the action you want taken if there is a filename conflict, as defined in the downloads.FilenameConflictAction
type (defaults to "uniquify" when it is not specified).
cookieStoreId
Optional
-
The cookie store ID of the contextual identity the download is associated with. If omitted, the default cookie store is used. Use requires the "cookies" API permission.
filename
Optional
-
A string
representing a file path relative to the default downloads directory — this provides the location where you want the file to be saved, and what filename you want to use. Absolute paths, empty paths, path components that start and/or end with a dot (.), and paths containing back-references (../
) will cause an error. If omitted, this value will default to the filename already given to the download file, and a location immediately inside the downloads directory.
-
If the URL uses the HTTP or HTTPS protocols, an array
of objects
representing additional HTTP headers to send with the request. Each header is represented as a dictionary object containing the keys name
and either value
or binaryValue
. The headers that are forbidden by XMLHttpRequest
and fetch
cannot be specified, however, Firefox 70 and later enables the use of the Referer
header. Attempting to use a forbidden header throws an error.
incognito
Optional
-
A boolean
: if present and set to true, then associate this download with a private browsing session. This means that it will only appear in the download manager for any private windows that are currently open.
method
Optional
-
A string
representing the HTTP method to use if the url
uses the HTTP[S] protocol. This may be either "GET" or "POST".
saveAs
Optional
-
A boolean
that specifies whether to provide a file chooser dialog to allow the user to select a filename (true
), or not (false
).
If this option is omitted, the browser will show the file chooser or not based on the general user preference for this behavior (in Firefox this preference is labeled "Always ask you where to save files" in about:preferences, or browser.download.useDownloadDir
in about:config).
Note: Firefox for Android raises an error if saveAs
is set to true
. The parameter is ignored when saveAs
is false
or not included.
-
url
-
A string
representing the URL to download.
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
download |
Yes |
79 |
47 |
? |
Yes |
No |
? |
? |
48–79 |
? |
No |
? |
allowHttpErrors |
No |
No |
71 |
? |
No |
No |
? |
? |
No |
? |
No |
? |
body |
Yes |
79 |
52 |
? |
Yes |
No |
? |
? |
52–79 |
? |
No |
? |
conflictAction |
Yes |
79 |
47 |
? |
Yes |
No |
? |
? |
48–79 |
? |
No |
? |
cookieStoreId |
No |
No |
92 |
? |
No |
No |
? |
? |
No |
? |
No |
? |
filename |
Yes |
79 |
47 |
? |
Yes |
No |
? |
? |
48–79 |
? |
No |
? |
headers |
Yes |
79 |
47Referer headers supported from version 70.
|
? |
Yes |
No |
? |
? |
48–79 |
? |
No |
? |
incognito |
No |
No |
57 |
? |
No |
No |
? |
? |
57–79 |
? |
No |
? |
method |
Yes |
79 |
47POST is supported from version 52.
|
? |
Yes |
No |
? |
? |
48–79POST is supported from version 52.
|
? |
No |
? |
saveAs |
Yes |
79 |
52Before version 58, if this option was omitted, Firefox would never show the file chooser, regardless of the value of the browser's preference.
|
? |
Yes |
No |
? |
? |
No |
? |
No |
? |
Examples
The following snippet attempts to download an example file, also specifying a filename and location to save it in, and uniquify
as the value of the conflictAction
option.
function onStartedDownload(id) {
console.log(`Started downloading: ${id}`);
}
function onFailed(error) {
console.log(`Download failed: ${error}`);
}
let downloadUrl = "https://example.org/image.png";
let downloading = browser.downloads.download({
url : downloadUrl,
filename : 'my-image-again.png',
conflictAction : 'uniquify'
});
downloading.then(onStartedDownload, onFailed);