Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
85 lines (72 loc) · 3.2 KB
/
Copy pathindex.md
File metadata and controls
85 lines (72 loc) · 3.2 KB
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
---
title: "XMLHttpRequest: upload property"
short-title: upload
slug: Web/API/XMLHttpRequest/upload
page-type: web-api-instance-property
browser-compat: api.XMLHttpRequest.upload
---
{{APIRef("XMLHttpRequest API")}} {{AvailableInWorkers("window_and_worker_except_service")}}
The **`upload`** read-only property of the {{domxref("XMLHttpRequest")}} interface returns an {{domxref("XMLHttpRequestUpload")}} object that can be observed to monitor an upload's progress.
It is an opaque object, but because it's also an {{domxref("XMLHttpRequestEventTarget")}}, event listeners can be attached to track its process.
> [!NOTE]
> Attaching event listeners to this object prevents the request from being a "simple request" and will cause a preflight request to be issued if cross-origin; see [CORS](/en-US/docs/Web/HTTP/Guides/CORS). Because of this, event listeners need to be registered before calling {{domxref("XMLHttpRequest.send", "send()")}} or upload events won't be dispatched.
> [!NOTE]
> The spec also seems to indicate that event listeners should be attached after {{domxref("XMLHttpRequest.open", "open()")}}. However, browsers are buggy on this matter, and often need the listeners to be registered _before_ {{domxref("XMLHttpRequest.open", "open()")}} to work.
The following events can be triggered on an upload object and used to monitor the upload:
<table class="no-markdown">
<thead>
<tr>
<th>Event</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{domxref("XMLHttpRequestEventTarget/loadstart_event", "loadstart")}}</td>
<td>The upload has begun.</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequestEventTarget/progress_event", "progress")}}</td>
<td>
Periodically delivered to indicate the amount of progress made so far.
</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequestEventTarget/abort_event", "abort")}}</td>
<td>The upload operation was aborted.</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequestEventTarget/error_event", "error")}}</td>
<td>The upload failed due to an error.</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequestEventTarget/load_event", "load")}}</td>
<td>The upload completed successfully.</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequestEventTarget/timeout_event", "timeout")}}</td>
<td>
The upload timed out because a reply did not arrive within the time
interval specified by the
{{domxref("XMLHttpRequest.timeout")}}.
</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequestEventTarget/loadend_event", "loadend")}}</td>
<td>
The upload finished. This event does not differentiate between success
or failure, and is sent at the end of the upload regardless of the
outcome. Prior to this event, one of <code>load</code>,
<code>error</code>, <code>abort</code>, or <code>timeout</code> will
already have been delivered to indicate why the upload ended.
</td>
</tr>
</tbody>
</table>
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Using XMLHttpRequest](/en-US/docs/Web/API/XMLHttpRequest_API/Using_XMLHttpRequest)
- {{domxref("XMLHttpRequestUpload")}}