Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document XMLHttpRequestUpload #25671

Merged
merged 24 commits into from Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion files/en-us/web/api/xmlhttprequest/send/index.md
Expand Up @@ -81,7 +81,7 @@ xhr.send(null);
const xhr = new XMLHttpRequest();
xhr.open("POST", '/server', true);

//Send the proper header information along with the request
// Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.onreadystatechange = () => { // Call a function when the state changes.
Expand Down
16 changes: 8 additions & 8 deletions files/en-us/web/api/xmlhttprequest/upload/index.md
Expand Up @@ -26,37 +26,37 @@ The following events can be triggered on an upload object and used to monitor th
</thead>
<tbody>
<tr>
<td>{{domxref("XMLHttpRequest/loadstart_event", "loadstart")}}</td>
<td>{{domxref("XMLHttpRequestUpload/loadstart_event", "loadstart")}}</td>
<td>The upload has begun.</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequest/progress_event", "progress")}}</td>
<td>{{domxref("XMLHttpRequestUpload/progress_event", "progress")}}</td>
<td>
Periodically delivered to indicate the amount of progress made so far.
</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequest/abort_event", "abort")}}</td>
<td>{{domxref("XMLHttpRequestUpload/abort_event", "abort")}}</td>
<td>The upload operation was aborted.</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequest/error_event", "error")}}</td>
<td>{{domxref("XMLHttpRequestUpload/error_event", "error")}}</td>
<td>The upload failed due to an error.</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequest/load_event", "load")}}</td>
<td>{{domxref("XMLHttpRequestUpload/load_event", "load")}}</td>
<td>The upload completed successfully.</td>
</tr>
<tr>
<td>{{domxref("XMLHttpRequest/timeout_event", "timeout")}}</td>
<td>{{domxref("XMLHttpRequestUpload/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("XMLHttpRequest/loadend_event", "loadend")}}</td>
<td>{{domxref("XMLHttpRequestUpload/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
Expand All @@ -79,4 +79,4 @@ The following events can be triggered on an upload object and used to monitor th
## See also

- [Using XMLHttpRequest](/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest)
- [File and Directory Entries API](/en-US/docs/Web/API/File_and_Directory_Entries_API)
- {{domxref("XMLHttpRequestUpload")}}
1 change: 1 addition & 0 deletions files/en-us/web/api/xmlhttprequesteventtarget/index.md
Expand Up @@ -23,6 +23,7 @@ The following events are made available to {{domxref("XMLHttpRequest")}}:
- {{domxref("XMLHttpRequest/loadend_event", "loadend")}}
- {{domxref("XMLHttpRequest/loadstart_event", "loadstart")}}
- {{domxref("XMLHttpRequest/progress_event", "progress")}}
- {{domxref("XMLHttpRequest/readystatechange_event", "readystatechange")}}
- {{domxref("XMLHttpRequest/timeout_event", "timeout")}}

## Inheritance for `XMLHttpRequestUpload`
Expand Down
66 changes: 66 additions & 0 deletions files/en-us/web/api/xmlhttprequestupload/abort_event/index.md
@@ -0,0 +1,66 @@
---
title: "XMLHttpRequestUpload: abort event"
slug: Web/API/XMLHttpRequestUpload/abort_event
page-type: web-api-event
browser-compat: api.XMLHttpRequestUpload.abort_event
---

{{APIRef}}

The `abort` event is fired at {{domxref("XMLHttpRequestUpload")}} when a request has been aborted, for example because the program called {{domxref("XMLHttpRequest.abort()")}}.

## Syntax

Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.

```js
addEventListener('abort', (event) => { })

onabort = (event) => { }
```

## Event type

A {{domxref("ProgressEvent")}}. Inherits from {{domxref("Event")}}.

{{InheritanceDiagram("ProgressEvent")}}

## Event properties

_In addition to the properties listed below, properties from the parent interface, {{domxref("Event")}}, are available._

- {{domxref("ProgressEvent.lengthComputable", "lengthComputable")}} {{ReadOnlyInline}}
- : A boolean flag indicating if the total work to be done, and the amount of work already done, by the underlying process is calculable. In other words, it tells if the progress is measurable or not.
- {{domxref("ProgressEvent.loaded", "loaded")}} {{ReadOnlyInline}}
- : A 64-bit unsigned integer value indicating the amount of work already performed by the underlying process. The ratio of work done can be calculated by dividing `total` by the value of this property. When downloading a resource using HTTP, this only counts the body of the HTTP message, and doesn't include headers and other overhead.
- {{domxref("ProgressEvent.total", "total")}} {{ReadOnlyInline}}
- : A 64-bit unsigned integer representing the total amount of work that the underlying process is in the progress of performing. When downloading a resource using HTTP, this is the `Content-Length` (the size of the body of the message), and doesn't include the headers and other overhead.

## Examples

### Using the `abort` event

You can use the `abort` event to stop the upload before it finishes. For a complete code example that uploads a file and displays a progress bar, see the main {{domxref("XMLHttpRequestUpload")}} page.

```js
// In case of an abort we hide the progress bar
// Note that this event can be listened to on the xhr object too
function errorAction(event) {
progressBar.classList.remove("visible");
log.textContent = `Upload failed: ${event.type}`;
}
xhr.upload.addEventListener("abort", errorAction);
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- Related events: {{domxref("XMLHttpRequestUpload/loadstart_event", "loadstart")}}, {{domxref("XMLHttpRequestUpload/load_event", "load")}}, {{domxref("XMLHttpRequestUpload/progress_event", "progress")}}, {{domxref("XMLHttpRequestUpload/error_event", "error")}}, {{domxref("XMLHttpRequestUpload/loadend_event", "loadend")}}, {{domxref("XMLHttpRequestUpload/timeout_event", "timeout")}}
- {{domxref("XMLHttpRequestUpload")}}
66 changes: 66 additions & 0 deletions files/en-us/web/api/xmlhttprequestupload/error_event/index.md
@@ -0,0 +1,66 @@
---
title: "XMLHttpRequestUpload: error event"
slug: Web/API/XMLHttpRequestUpload/error_event
page-type: web-api-event
browser-compat: api.XMLHttpRequestUpload.error_event
---

{{APIRef}}

The `error` event is fired when the request encountered an error.

## Syntax

Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.

```js
addEventListener('error', (event) => { })

onerror = (event) => { }
```

## Event type

A {{domxref("ProgressEvent")}}. Inherits from {{domxref("Event")}}.

{{InheritanceDiagram("ProgressEvent")}}

## Event properties

_In addition to the properties listed below, properties from the parent interface, {{domxref("Event")}}, are available._

- {{domxref("ProgressEvent.lengthComputable", "lengthComputable")}} {{ReadOnlyInline}}
- : A boolean flag indicating if the total work to be done, and the amount of work already done, by the underlying process is calculable. In other words, it tells if the progress is measurable or not.
- {{domxref("ProgressEvent.loaded", "loaded")}} {{ReadOnlyInline}}
- : A 64-bit unsigned integer value indicating the amount of work already performed by the underlying process. The ratio of work done can be calculated by dividing `total` by the value of this property. When downloading a resource using HTTP, this only counts the body of the HTTP message, and doesn't include headers and other overhead.
- {{domxref("ProgressEvent.total", "total")}} {{ReadOnlyInline}}
- : A 64-bit unsigned integer representing the total amount of work that the underlying process is in the progress of performing. When downloading a resource using HTTP, this is the `Content-Length` (the size of the body of the message), and doesn't include the headers and other overhead.

## Examples

### Using the `error` event

You can use the `error` event to detect a problem with the upload. For a complete code example that uploads a file and displays a progress bar, see the main {{domxref("XMLHttpRequestUpload")}} page.

```js
// In case of an error we hide the progress bar
// Note that this event can be listened to on the xhr object too
function errorAction(event) {
progressBar.classList.remove("visible");
log.textContent = `Upload failed: ${event.type}`;
}
xhr.upload.addEventListener("error", errorAction);
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- Related events: {{domxref("XMLHttpRequestUpload/loadstart_event", "loadstart")}}, {{domxref("XMLHttpRequestUpload/load_event", "load")}}, {{domxref("XMLHttpRequestUpload/progress_event", "progress")}}, {{domxref("XMLHttpRequestUpload/abort_event", "abort")}}, {{domxref("XMLHttpRequestUpload/loadend_event", "loadend")}}, {{domxref("XMLHttpRequestUpload/timeout_event", "timeout")}}
- {{domxref("XMLHttpRequestUpload")}}