The download()
function of the downloads
API downloads a file, given its URL and other optional preferences.
Other notes to bear in mind about download()
:
- If the specified
url
uses the HTTP or HTTPS protocol, then the request will include all cookies currently set for its hostname. - If both
filename
andsaveAs
are specified, then the Save As dialog will be displayed, pre-populated with the specifiedfilename
. - If the download started successfully and a
callback
was specified, it will be run with the newDownloadItem
'sid
as a parameter. - If there was an error starting the download and a
callback
was specified, it will be called with adownloadId
parameter with a value ofundefined.
- In any error case,
runtime.lastError
will contain a descriptive string. The error strings are not guaranteed to stay the same between releases.
Syntax
chrome.downloads.download( options, // object function(downloadId) {...} // optional function )
This API is also available as browser.downloads.download()
in a version that returns a promise.
Parameters
options
- An
object
specifying what file you wish to download, and any other preferences you wish to set concerning the download. It can contain the following properties: -
url
- A
string
representing the URL to download. filename
Optional- 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, 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. conflictAction
Optional- A string representing the action you want taken if there is a filename conflict, as defined in the
downloads.FilenameConflictAction
type. saveAs
Optional- A
boolean
that specifies whether to provide a file chooser dialog to allow the user to select a filename (true
), or not (false
). method
Optional- A
string
representing the HTTP method to use if theurl
uses the HTTP[S] protocol. headers
Optional- An
array
ofobjects
representing extra HTTP headers to send with the request if the URL uses the HTTP[s] protocol. Each header is represented as a dictionary object containing the keysname
and eithervalue
orbinaryValue
, restricted to those allowed byXMLHttpRequest
. body
Optional- A
string
representing the post body of the request.
function() {}
Optional- An optional callback function, which, if included, is run when the download successfully starts. The function is passed the following arguments:
-
downloadId
- An
integer
representing theid
of the newdownloads.DownloadItem
.
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Chrome | Edge | Firefox | Firefox for Android | Opera | |
---|---|---|---|---|---|
Basic support | Yes | No | 47.0 | 48.0 | 33 |
Compatibility notes
Firefox
- 'saveAs' is not supported.
- 'GET' is the only supported option for 'method'.
Examples
The following snippet attempts to download an example file, also specifying a filename and location to save it in, and the uniquify
conflictAction
option.
A callback function is defined to run when the download is started. Inside here we check whether runtime.lastError
has been set, and log it to the console if is. If not, we log some information about the downloaded file to the console, after retrieving it by passing its id
to downloads.search()
.
var btn = document.querySelector('button'); btn.onclick = function() { chrome.downloads.download({ url : 'https://www.example.com/myImage.png', filename : 'images/myImage.png', conflictAction : 'uniquify' }, callback); function callback(downloadId) { if(chrome.runtime.lastError) { console.log(chrome.runtime.lastError); } else { chrome.downloads.search({ id : downloadId }, function(results) { if(results[0].totalBytes === -1) { console.log('File downloading — ' + 'file size unknown; ' + 'the download was started at ' + results[0].startTime + '.'); } else { console.log('File downloading — ' + 'it is ' + results[0].totalBytes + ' bytes in size, ' + ' and the download was started at ' + results[0].startTime + '.'); } }); } } }
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.
// Copyright 2015 The Chromium Authors. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.