Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

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.

Note: To use this function in your WebExtension you must ask for the "downloads.open" manifest permission, as well as the "downloads" permission.

Syntax

chrome.downloads.open(
  downloadId,      // integer
  function() {...} // optional function
)

This API is also available as browser.downloads.open() in a version that returns a promise.

Parameters

downloadId
An integer representing the id of the downloads.DownloadItem you want to open.
function() {}Optional
An optional callback function that gets invoked, with no parameters, when the open() call has completed.

Browser compatibility

Chrome Edge Firefox Firefox for Android Opera
Basic support Yes No 48.0 48.0 33

Examples

The following snippet shows two buttons, "download" and "open". The first button when clicked initiates a download using downloads.download(). Once the download has started, the second button can be pressed to open the file with its associated application.

var downloadBtn = document.querySelector('.download');
var openBtn = document.querySelector('.open');

downloadBtn.onclick = function() {
  chrome.downloads.download({ url : 'https://www.example.com/myBigVideo.mp4'},
  callback);

  function callback(downloadId) {
    if(chrome.runtime.lastError) {
      console.log(chrome.runtime.lastError);
    } else {
      openBtn.onclick = function() {
        chrome.downloads.open(downloadId, function() {
          console.log('File opened');
        });
      }
    }
  }
}

Example add-ons

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.

文档标签和贡献者

 此页面的贡献者: Makyen, wbamberg, chrisdavidmills
 最后编辑者: Makyen,