aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/formdata/append/index.html
blob: 253ec34e4de81cef840a7133de0df5a75fe6bb5e (plain)
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
---
title: FormData.append()
slug: Web/API/FormData/append
translation_of: Web/API/FormData/append
---
<p>{{APIRef("XMLHttpRequest")}}</p>

<p>インターフェイスの<code><strong>append()</strong></code>メソッドは、FormDataオブジェクト内の既存のキーに新しい値を追加するか、キーがまだ存在しない場合は追加します。</p>

<p>{{domxref("FormData.set")}}との違いは、指定されたキーが既に存在する場合、{{domxref("FormData.set")}}はすべての既存の値を新しい値で上書きすることです。 一方、<code>append()</code>は、既存の値のセットの最後に新しい値を追加します。</p>

<div class="note">
<p><strong>注:このメソッドはWeb Workersで使用できます。</strong></p>
</div>

<h2 id="Syntax">Syntax</h2>

<p>There are two versions of this method: a two and a three parameter version:</p>

<pre class="brush: js">formData.append(name, value);
formData.append(name, value, filename);</pre>

<h3 id="append_Parameters" name="append()_Parameters">Parameters</h3>

<dl>
 <dt><code>name</code></dt>
 <dd>The name of the field whose data is contained in <code>value</code>.</dd>
 <dt><code>value</code></dt>
 <dd>The field's value. This can be a {{domxref("USVString")}} or {{domxref("Blob")}} (including subclasses such as {{domxref("File")}}). If none of these are specified the value is converted to a string.</dd>
 <dt><code>filename </code>{{optional_inline}}</dt>
 <dd>The filename reported to the server (a {{domxref("USVString")}}), when a {{domxref("Blob")}} or {{domxref("File")}} is passed as the second parameter. The default filename for {{domxref("Blob")}} objects is "blob". The default filename for {{domxref("File")}} objects is the file's filename.</dd>
</dl>

<div class="note">
<p><strong>Note:</strong> If you specify a {{domxref("Blob")}} as the data to append to the <code>FormData</code> object, the filename that will be reported to the server in the "Content-Disposition" header used to vary from browser to browser.</p>
</div>

<h3 id="Returns">Returns</h3>

<p>Void.</p>

<h2 id="Example">Example</h2>

<p>The following line creates an empty <code>FormData</code> object:</p>

<pre class="brush: js">var formData = new FormData(); // Currently empty</pre>

<p>You can add key/value pairs to this using {{domxref("FormData.append")}}:</p>

<pre class="brush: js">formData.append('username', 'Chris');
formData.append('userpic', myFileInput.files[0], 'chris.jpg');</pre>

<p>As with regular form data, you can append multiple values with the same name. For example (and being compatible with PHP's naming conventions by adding [] to the name):</p>

<pre class="brush: js">formData.append('userpic[]', myFileInput.files[0], 'chris1.jpg');
formData.append('userpic[]', myFileInput.files[1], 'chris2.jpg');</pre>

<p>This technique makes it simpler to process multi-file uploads because the resultant data structure is more conducive to looping.</p>

<p>If the sent value is different than String or Blob it will be automatically converted to String:</p>

<pre class="brush: js">formData.append('name', true);
formData.append('name', 74);
formData.append('name', 'John');

formData.getAll('name'); // ["true", "74", "John"]
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('XMLHttpRequest','#dom-formdata-append','append()')}}</td>
   <td>{{Spec2('XMLHttpRequest')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="ブラウザの互換性">ブラウザの互換性</h2>

<div class="hidden"></div>

<div class="hidden"></div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("XMLHTTPRequest")}}</li>
 <li><a href="/ja/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest" title="Using XMLHttpRequest">Using XMLHttpRequest</a></li>
 <li><a href="/ja/docs/DOM/XMLHttpRequest/FormData/Using_FormData_Objects" title="DOM/XMLHttpRequest/FormData/Using_FormData_objects">Using FormData objects</a></li>
 <li>{{HTMLElement("Form")}}</li>
</ul>