回答編集履歴
1
サンプルプログラム追加
test
CHANGED
@@ -173,3 +173,85 @@
|
|
173
173
|
(略)
|
174
174
|
|
175
175
|
```
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
## サンプルプログラム
|
180
|
+
|
181
|
+
```html
|
182
|
+
|
183
|
+
<html>
|
184
|
+
|
185
|
+
<body>
|
186
|
+
|
187
|
+
<form id="form1" method="post" action="/upload">
|
188
|
+
|
189
|
+
<input type="file" id="file" name="file" onchange="handleFiles(this.files)" />
|
190
|
+
|
191
|
+
</form>
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
<script>
|
196
|
+
|
197
|
+
function handleFiles(files) {
|
198
|
+
|
199
|
+
var file = files[0];
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
// ArrayBuffer
|
204
|
+
|
205
|
+
var reader = new FileReader();
|
206
|
+
|
207
|
+
reader.readAsArrayBuffer(file);
|
208
|
+
|
209
|
+
reader.onload = function () {
|
210
|
+
|
211
|
+
upload(reader.result, file.type);
|
212
|
+
|
213
|
+
};
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
// Blob
|
218
|
+
|
219
|
+
upload(file);
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
// FormData
|
224
|
+
|
225
|
+
var formData = new FormData();
|
226
|
+
|
227
|
+
formData.append("image", file);
|
228
|
+
|
229
|
+
upload(formData);
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
function upload(data, type = null) {
|
236
|
+
|
237
|
+
var xhr = new XMLHttpRequest();
|
238
|
+
|
239
|
+
xhr.open(form1.method, form1.action);
|
240
|
+
|
241
|
+
if (type !== null) {
|
242
|
+
|
243
|
+
xhr.setRequestHeader('Content-Type', type);
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
xhr.send(data);
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
</script>
|
252
|
+
|
253
|
+
</body>
|
254
|
+
|
255
|
+
</html>
|
256
|
+
|
257
|
+
```
|