downloads.download()

download() function of the 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:

  • the incognito option is used, then the private browsing cookies are used.
  • the cookieStoreId option is used, then the cookies from the specified store are used.

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 .

句法

var downloading = browser.downloads.download(
选项

// object


)


					

参数

选项

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:

allowHttpErrors 可选

A boolean 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 false . When set to:

  • false , the download is canceled when it encounters an HTTP error.
  • true , 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.
body 可选

A string representing the post body of the request.

conflictAction 可选

A string representing the action you want taken if there is a filename conflict, as defined in the downloads.FilenameConflictAction type (defaults to "uniquify" when it is not specified).

cookieStoreId 可选

The cookie store ID of the contextual identity the download is associated with. If omitted, the default cookie store is used. Use requires the "cookies" API permission .

filename 可选

A string 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.

headers 可选

If the URL uses the HTTP or HTTPS protocols, an array of 对象 representing additional HTTP headers to send with the request. Each header is represented as a dictionary object containing the keys 名称 and either value or binaryValue . The headers that are forbidden by XMLHttpRequest and fetch cannot be specified, however, Firefox 70 and later enables the use of the Referer header. Attempting to use a forbidden header throws an error.

incognito 可选

A boolean : 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.

方法 可选

A string representing the HTTP method to use if the url uses the HTTP[S] protocol. This may be either "GET" or "POST".

saveAs 可选

A boolean that specifies whether to provide a file chooser dialog to allow the user to select a filename ( true ), or not ( false ).

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 browser.download.useDownloadDir in about:config).

注意: Firefox for Android raises an error if saveAs 被设为 true . The parameter is ignored when saveAs is false or not included.

url

A string representing the URL to download.

返回值

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

若使用 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 事件。

浏览器兼容性

BCD tables only load in the browser

范例

The following snippet attempts to download an example file, also specifying a filename and location to save it in, and the uniquify conflictAction 选项。

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

					

注意: This API is based on Chromium's chrome.downloads API。

Found a problem with this page?

最后修改: , 由 MDN 贡献者

  1. 浏览器扩展名
  2. 快速入门
    1. What are extensions?
    2. Your first extension
    3. Your second extension
    4. Anatomy of an extension
    5. Example extensions
    6. What next?
  3. 概念
    1. Using the JavaScript APIs
    2. Content scripts
    3. Match patterns
    4. Working with files
    5. 国际化
    6. Content Security Policy
    7. Native messaging
    8. Differences between API implementations
    9. Chrome incompatibilities
  4. 用户界面
    1. 用户界面
    2. Toolbar button
    3. Address bar button
    4. Sidebars
    5. Context menu items
    6. Options page
    7. Extension pages
    8. Notifications
    9. Address bar suggestions
    10. Developer tools panels
  5. 如何
    1. Intercept HTTP requests
    2. Modify a web page
    3. Insert external content
    4. Share objects with page scripts
    5. Add a button to the toolbar
    6. Implement a settings page
    7. Work with the Tabs API
    8. Work with the Bookmarks API
    9. Work with the Cookies API
    10. Work with contextual identities
    11. Interact with the clipboard
    12. Build a cross-browser extension
  6. Firefox differentiators
  7. JavaScript API
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browserSettings
    6. browsingData
    7. captivePortal
    8. clipboard
    9. 命令
    10. contentScripts
    11. contextualIdentities
    12. Cookie
    13. devtools
    14. dns
    15. downloads
      1. 方法
        1. acceptDanger()
        2. cancel()
        3. download()
        4. drag()
        5. erase()
        6. getFileIcon()
        7. open()
        8. pause()
        9. removeFile()
        10. resume()
        11. search()
        12. setShelfEnabled()
        13. show()
        14. showDefaultFolder()
      2. 类型
        1. BooleanDelta
        2. DangerType
        3. DoubleDelta
        4. DownloadItem
        5. DownloadQuery
        6. DownloadTime
        7. FilenameConflictAction
        8. InterruptReason
        9. 状态
        10. StringDelta
      3. 事件
        1. onChanged
        2. onCreated
        3. onErased
    16. events
    17. extension
    18. extensionTypes
    19. find
    20. history
    21. i18n
    22. identity
    23. idle
    24. management
    25. menus
    26. notifications
    27. omnibox
    28. pageAction
    29. permissions
    30. pkcs11
    31. privacy
    32. proxy
    33. runtime
    34. search
    35. sessions
    36. sidebarAction
    37. storage
    38. tabs
    39. theme
    40. topSites
    41. 类型
    42. userScripts
    43. webNavigation
    44. webRequest
    45. windows
  8. Manifest keys
    1. 介绍
    1. 作者
    2. background
    3. browser_action
    4. browser_specific_settings
    5. chrome_settings_overrides
    6. chrome_url_overrides
    7. 命令
    8. content_scripts
    9. content_security_policy
    10. default_locale
    11. description
    12. developer
    13. devtools_page
    14. dictionaries
    15. externally_connectable
    16. homepage_url
    17. icons
    18. incognito
    19. manifest_version
    20. name
    21. offline_enabled
    22. omnibox
    23. optional_permissions
    24. options_page
    25. options_ui
    26. page_action
    27. permissions
    28. protocol_handlers
    29. short_name
    30. sidebar_action
    31. storage
    32. theme
    33. theme_experiment
    34. user_scripts
    35. version
    36. version_name
    37. web_accessible_resources
  9. Extension Workshop
    1. Develop
    2. Publish
    3. Manage
    4. Enterprise
  10. Contact us
  11. Channels
    1. Add-ons blog
    2. Add-ons forum
    3. Add-ons chat