downloads.download()
The function of the API downloads a file, given its URL and other optional preferences.
- If the specified uses the HTTP or HTTPS protocol, then the request will include all cookies currently set for its hostname.
- If both and are specified, then the Save As dialog will be displayed, pre-populated with the specified .
This is an asynchronous function that returns a .
Syntax
var downloading = browser.downloads.download( options // object )Parameters
- An specifying what file you wish to download, and any other preferences you wish to set concerning the download. It can contain the following properties:
- Optional
- A 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 . When set to:
- , the download is canceled when it encounters an HTTP error.
- , 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.
- Optional
- A representing the post body of the request.
- Optional
- A string representing the action you want taken if there is a filename conflict, as defined in the type (defaults to "uniquify" when it is not specified).
- Optional
- A 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.
- Optional
- If the URL uses the HTTP or HTTPS protocols, an of representing additional HTTP headers to send with the request. Each header is represented as a dictionary object containing the keys and either or . The headers that are forbidden by and cannot be specified, however, Firefox 70 and later enables the use of the header. Attempting to use a forbidden header throws an error.
- Optional
- A : 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.
- Optional
- A representing the HTTP method to use if the uses the HTTP[S] protocol. This may be either "GET" or "POST".
- Optional
A that specifies whether to provide a file chooser dialog to allow the user to select a filename (), or not ().
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 in about:config).
Note: Firefox for Android raises an error if is set to . The parameter is ignored when is or not included.
- A representing the URL to download.
Return value
A . If the download started successfully, the promise will be fulfilled with the of the new . Otherwise, the promise will be rejected with an error message taken from .
If you use URL.createObjectURL() to download data created in JavaScript and you want to revoke the object URL (with revokeObjectURL) later (as it is strongly recommended), you need to do that after the download has been completed. To do so, listen to the downloads.onChanged event.
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | ||||
---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Firefox for Android |
ChromeFull support Yes | EdgeFull support 79 | FirefoxFull support 47 | OperaFull support Yes | SafariNo support No | Firefox AndroidNo support48 — 79 |
ChromeNo support No | EdgeNo support No | FirefoxFull support 71 | OperaNo support No | SafariNo support No | Firefox AndroidNo support No |
ChromeFull support Yes | EdgeFull support 79 | FirefoxFull support 52 | OperaFull support Yes | SafariNo support No | Firefox AndroidNo support52 — 79 |
ChromeFull support Yes | EdgeFull support 79 | FirefoxFull support 47 | OperaFull support Yes | SafariNo support No | Firefox AndroidNo support48 — 79 |
ChromeFull support Yes | EdgeFull support 79 | FirefoxFull support 47 | OperaFull support Yes | SafariNo support No | Firefox AndroidNo support48 — 79 |
ChromeFull support Yes | EdgeFull support 79 | FirefoxFull support 47
| OperaFull support Yes | SafariNo support No | Firefox AndroidNo support48 — 79 |
ChromeNo support No | EdgeNo support No | FirefoxFull support 57 | OperaNo support No | SafariNo support No | Firefox AndroidNo support57 — 79 |
ChromeFull support Yes | EdgeFull support 79 | FirefoxFull support 47
| OperaFull support Yes | SafariNo support No | Firefox AndroidNo support48 — 79
|
ChromeFull support Yes | EdgeFull support 79 | FirefoxNo support52 — 79
| OperaFull support Yes | SafariNo support No | Firefox AndroidNo support No |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.
Examples
The following snippet attempts to download an example file, also specifying a filename and location to save it in, and the option.
function onStartedDownload(id) { console.log(`Started downloading: ${id}`); } function onFailed(error) { console.log(`Download failed: ${error}`); } var downloadUrl = "https://example.org/image.png"; var downloading = browser.downloads.download({ url : downloadUrl, filename : 'my-image-again.png', conflictAction : 'uniquify' }); downloading.then(onStartedDownload, onFailed);
-