# Simple upload adapter

The simple upload adapter lets you upload images to your server using the [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) API with a minimal [editor configuration](#configuration). See the [Server-side configuration](#server-side-configuration) section to learn about the requirements for your server-side application.

> **Unlock this feature with selected CKEditor Plans**
>
> Try all premium features – no credit card needed.
>
> [Sign up for a free trial ](https://portal.ckeditor.com/checkout?plan=free)[Select a Plan](https://ckeditor.com/pricing/)

<a id="installation">

## Installation

After [installing the editor](../../../getting-started/installation/cloud/quick-start.md), add the feature to your plugin list and toolbar configuration:

**NPM**

```js
import { ClassicEditor, SimpleUploadAdapter } from 'ckeditor5';

ClassicEditor
	.create( {
		licenseKey: '<YOUR_LICENSE_KEY>', // Or 'GPL'.
		plugins: [ SimpleUploadAdapter, /* ... */ ],
		toolbar: [ /* ... */ ],
		simpleUpload: {
			// Configuration.
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );
```

**CDN**

```js
const { ClassicEditor, SimpleUploadAdapter } = CKEDITOR;

ClassicEditor
	.create( {
		licenseKey: '<YOUR_LICENSE_KEY>',
		plugins: [ SimpleUploadAdapter, /* ... */ ],
		toolbar: [ /* ... */ ],
		simpleUpload: {
			// Configuration.
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );
```

<a id="configuration">

## Configuration

The client side of this feature is configurable using the [`config.simpleUpload`](../../../api/module_upload_uploadconfig-SimpleUploadConfig.md) object.

```js
ClassicEditor
	.create( {
		// ... Other configuration options ...
		simpleUpload: {
			// The URL that the images are uploaded to.
			uploadUrl: 'http://example.com',

			// Enable the XMLHttpRequest.withCredentials property.
			withCredentials: true,

			// Headers sent along with the XMLHttpRequest to the upload server.
			headers: {
				'X-CSRF-TOKEN': 'CSRF-Token',
				Authorization: 'Bearer <JSON Web Token>'
			}
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );
```

<a id="configuring-allowed-file-types">

### Configuring allowed file types

The allowed file types that can be uploaded should be configured in two places:

* On the client side, in CKEditor 5, restricting image upload through the CKEditor 5 UI and commands.
* On the server side, in your server-side application configuration.

<a id="client-side-configuration">

#### Client-side configuration

Use the [`image.upload.types`](../../../api/module_image_imageconfig-ImageUploadConfig.md#member-types) configuration option to define the allowed image MIME types that can be uploaded to CKEditor 5.

By default, users are allowed to upload `jpeg`, `png`, `gif`, `bmp`, `webp`, and `tiff` files, but you can customize this behavior to accept, for example, SVG files.

<a id="server-side-configuration">

#### Server-side configuration

It is up to you to implement any filtering mechanisms on your server to restrict the types of images that are allowed to be uploaded.

<a id="server-side-configuration-2">

## Server-side configuration

To use this upload adapter, you must provide a server-side application that will handle the uploads and communicate with the editor, as described in the following sections.

<a id="communication-protocol">

### Communication protocol

When the image upload process is initiated, the adapter sends a [`POST`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) request under [`config.simpleUpload.uploadUrl`](../../../api/module_upload_uploadconfig-SimpleUploadConfig.md#member-uploadUrl).

You can send additional [headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) along with the `XMLHttpRequest` to the upload server, for example, to authenticate the user, using the [`config.simpleUpload.headers`](../../../api/module_upload_uploadconfig-SimpleUploadConfig.md#member-uploadUrl) object.

If you use the [`config.simpleUpload.withCredentials`](../../../api/module_upload_uploadconfig-SimpleUploadConfig.md#member-withCredentials) configuration, you may need some [extra HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) for the cross-site request to work.

The [`responseType`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType) of the request is always `json`. See the [Successful upload](#successful-upload) and [Error handling](#error-handling) sections to learn more.

<a id="successful-upload">

### Successful upload

If the upload is successful, the server should return:

* An object containing the `url` property which points to the uploaded image on the server:

  ```json
  {
  	"url": "https://example.com/images/foo.jpg"
  }
  ```

* Or an object with the `urls` property, if you want to use [responsive images](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images) and the server supports it:

  ```json
  {
  	"urls": {
  		"default": "https://example.com/images/foo.jpg",
  		"800": "https://example.com/images/foo-800.jpg",
  		"1024": "https://example.com/images/foo-1024.jpg",
  		"1920": "https://example.com/images/foo-1920.jpg"
  	}
  }
  ```

  The `"default"` URL will be used in the `src` attribute of the image in the rich-text editor content. Other URLs will be used in the `srcset` attribute, allowing the web browser to select the best one for the geometry of the viewport.

The URL(s) in the server response are used:

* To display the image during the editing (as seen by the user in the editor).
* In the editor content [saved to the database](../../../getting-started/setup/getting-and-setting-data.md).

<a id="error-handling">

### Error handling

If something went wrong, the server must return an object that contains the `error` property. This will cancel the upload in the editor, for example, allowing the user to select another image if the previous one was too big or did not meet some other validation criteria.

If the `error` object contains a `message`, it will be passed to the [editor notification system](../../../api/module_ui_notification_notification-Notification.md#function-showWarning) and displayed to the user. For the convenience of the users, use clear and possibly specific error messages.

```json
{
	"error": {
		"message": "The image upload failed because the image was too big (max 1.5MB)."
	}
}
```

If the `message` property is missing in the `error` object, the [editor notification system](../../../api/module_ui_notification_notification-Notification.md#function-showWarning) will display the default “Could not upload file: `[filename]`.” message.

<a id="upload-progress">

### Upload progress

This upload adapter will notify users about the [file upload progress](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/progress_event) out–of–the–box.

<a id="whats-next">

## What’s next

Check out the comprehensive [Image upload overview](image-upload.md) to learn more about different ways of uploading images in CKEditor 5.

See the [Image feature guide](../images-overview.md) to find out more about handling images in CKEditor 5 WYSIWYG editor.

<a id="contribute">

## Contribute

The source code of the feature is available on GitHub at <https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-upload>.

---

Full index of the CKEditor 5 documentation: [llms.txt](../../../../llms.txt)
