Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mohamedkhallouq
GitHub Repository: mohamedkhallouq/content
Path: blob/main/files/en-us/mozilla/add-ons/webextensions/api/downloads/open/index.md
6583 views
---
title: downloads.open() slug: Mozilla/Add-ons/WebExtensions/API/downloads/open page-type: webextension-api-function tags: - API - Add-ons - Extensions - Method - Non-standard - Reference - WebExtensions - downloads - open browser-compat: webextensions.api.downloads.open
---

{{AddonSidebar()}}

The open() function of the {{WebExtAPIRef("downloads")}} API opens the downloaded file with its associated application. A {{WebExtAPIRef("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.

This is an asynchronous function that returns a Promise.

Syntax

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

Parameters

  • downloadId

    • : An integer representing the id of the {{WebExtAPIRef("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

{{Compat}}

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) { let opening = browser.downloads.open(downloadItems[0].id); opening.then(onOpened, onError); } } let searching = browser.downloads.search({ limit: 1, orderBy: ["-startTime"] }); searching.then(openDownload, onError);

{{WebExtExamples}}

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