1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
---
title: downloads.download()
slug: Mozilla/Add-ons/WebExtensions/API/downloads/download
translation_of: Mozilla/Add-ons/WebExtensions/API/downloads/download
---
<div>{{AddonSidebar()}}</div>
<p>{{WebExtAPIRef("downloads")}} API の <strong><code>download()</code></strong> 関数ではURLとそのほかのオプションの設定を行うことでファイルのダウンロードをすることができます。</p>
<ul>
<li>HTTPもしくはHTTPSのプロトコルを使用したURLを指定した場合、対象のホスト名に対応する全てのcookieを含んだリクエストが送られます。</li>
<li><code>filename</code> と <code>saveAs</code> が指定されている場合、指定された<code>filename</code>が設定された[名前をつけて保存]のダイアログが開きます。</li>
</ul>
<p>この関数は非同期に実行され、<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code>を返します。</p>
<h2 id="構文">構文</h2>
<pre class="syntaxbox brush:js">var downloading = browser.downloads.download(
options // object
)
</pre>
<h3 id="パラメータ">パラメータ</h3>
<dl>
<dt><code>options</code></dt>
<dd>この<code>object</code>ではダウンロードしたいファイルやその他のダウンロードに関する設定を指定します。指定できるプロパティは以下です。</dd>
<dd>
<dl class="reference-values">
<dt><code>body</code>{{optional_inline}}</dt>
<dd>リクエストのbodyを<code>string</code>で指定します。</dd>
<dt><code>conflictAction</code>{{optional_inline}}</dt>
<dd>A string representing the action you want taken if there is a filename conflict, as defined in the {{WebExtAPIRef('downloads.FilenameConflictAction')}} type (defaults to "uniquify" when it is not specified).</dd>
<dt><code>filename</code>{{optional_inline}}</dt>
<dd>A <code>string</code> 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 (<code>../</code>) 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.</dd>
<dt><code>headers</code>{{optional_inline}}</dt>
<dd>An <code>array</code> of <code>objects</code> 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 keys <code>name</code> and either <code>value</code> or <code>binaryValue</code>, restricted to those allowed by <code><a href="/en-US/docs/Web/API/XMLHttpRequest">XMLHttpRequest</a></code>.</dd>
<dt><code>incognito</code>{{optional_inline}}</dt>
<dd>A <code>boolean</code>: 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.</dd>
<dt><code>method</code>{{optional_inline}}</dt>
<dd>HTTP[S]を使用したURLを指定した際、HTTPメソッドを<code>string</code>で指定します。GETもしくはPOSTを設定できます。</dd>
<dt><code>saveAs</code>{{optional_inline}}</dt>
<dd>
<p>A <code>boolean</code> that specifies whether to provide a file chooser dialog to allow the user to select a filename (<code>true</code>), or not (<code>false</code>).</p>
<p>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 <code>browser.download.useDownloadDir</code> in about:config).</p>
</dd>
<dt><code>url</code></dt>
<dd>ダウンロードするURLを<code>string</code>で指定します。</dd>
</dl>
</dd>
</dl>
<h3 id="戻り値">戻り値</h3>
<p><code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code>が返却されます。ダウンロードが成功した場合、new {{WebExtAPIRef("downloads.DownloadItem")}}のidが格納されたpromiseを受け取ります。対して、promiseがrejectされた場合は、エラーメッセージを受け取ります。</p>
<h2 id="ブラウザ実装状況">ブラウザ実装状況</h2>
<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
<p> </p>
<p>{{Compat("webextensions.api.downloads.download")}}</p>
<p> </p>
<h2 id="例">例</h2>
<p>以下のダウンロードの例ではファイル名と保存場所を指定し、<code>conflictAction</code>に<code>uniquify</code>を指定しています。</p>
<pre class="brush: js">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);</pre>
<p>{{WebExtExamples}}</p>
<div class="note"><strong>Acknowledgements</strong>
<p>このAPIはChromiumの <a href="https://developer.chrome.com/extensions/downloads#method-download"><code>chrome.downloads</code></a> APIを元にしています。</p>
</div>
<div class="hidden">
<pre>// 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.
</pre>
</div>
|