docs.rodeo

MDN Web Docs mirror

downloads.download()

{{AddonSidebar}} 

The download() function of the {{WebExtAPIRef("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:

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                   // object
)

Parameters

Return value

A Promise. If the download started successfully, the promise will be fulfilled with the id of the new {{WebExtAPIRef("downloads.DownloadItem")}} . Otherwise, the promise will be rejected with an error message taken from {{WebExtAPIRef("downloads.InterruptReason")}} .

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

{{Compat}} 

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);

{{WebExtExamples}} 

[!NOTE] This API is based on Chromium’s chrome.downloads API.

In this article

View on MDN