Add-ons

downloads.open()

The open() function of the downloads API opens the downloaded file with its associated application. A downloads.onChanged event will fire when the item is opened for the first time.

To use this function in your extension you must ask for the "downloads.open" manifest permission, as well as the "downloads" permission. Also, you can only call this function from inside the handler for a user action, such as

  • clicking the extension's browser action or page action
  • selecting its context menu item
  • activating a keyboard shortcut defined by the extension (note: this is not currently supported in Firefox)
  • clicking a button in a page bundled with the extension.

This is an asynchronous function that returns a Promise.

Syntax

var opening = browser.downloads.open(
  downloadId      // integer
)

Parameters

downloadId
An integer representing the id of the downloads.DownloadItem you want to open.

Return value

A Promise. If the request was successful, the promise will be fulfilled with no arguments. If the request failed, the promise will be rejected with an error message.

Browser compatibility

ChromeEdgeFirefoxFirefox for AndroidOpera
Basic Support (Yes) No4848 (Yes)

Examples

This example opens the most recently downloaded item:

function onOpened() {
  console.log(`Opened download item`);
}

function onError(error) {
  console.log(`Error opening item: ${error}`);
}

function openDownload(downloadItems) {
    if (downloadItems.length > 0) {
      var opening = browser.downloads.open(downloadItems[0].id);
      opening.then(onOpened, onError);
    }
  }

var searching = browser.downloads.search({
  limit: 1,
  orderBy: ["-startTime"]
});

searching.then(openDownload, onError);

Example extensions

Acknowledgements

This API is based on Chromium's chrome.downloads API.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

Document Tags and Contributors

 Contributors to this page: wbamberg, andrewtruongmoz, Makyen, chrisdavidmills
 Last updated by: wbamberg,