category: features-image-upload menu-title: Simple upload adapter
The {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter Simple upload adapter} allows uploading images to your server using the XMLHttpRequest API with a minimal editor configuration.
See the "Server–side configuration" section to learn about the requirements your server–side application must meet to support this upload adapter.
Check out the comprehensive {@link features/image-upload Image upload overview} to learn about other ways to upload images into CKEditor 5.
First, install the @ckeditor/ckeditor5-upload package:
npm install --save @ckeditor/ckeditor5-upload
The [`@ckeditor/ckeditor5-upload`](https://www.npmjs.com/package/@ckeditor/ckeditor5-upload) package is available by default in all {@link builds/guides/overview#available-builds official editor builds}. You do not have to install it, if you are {@link builds/guides/integration/advanced-setup#scenario-1-integrating-existing-builds extending one}.
Add the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadAdapter SimpleUploadAdapter} to your plugin list and configure the feature. For instance:
import SimpleUploadAdapter from '@ckeditor/ckeditor5-upload/src/adapters/simpleuploadadapter';
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ SimpleUploadAdapter, ... ],
toolbar: [ ... ],
simpleUpload: {
// Feature configuration.
}
} )
.then( ... )
.catch( ... );
Read more about {@link builds/guides/integration/installing-plugins installing plugins}.
The client–side of this feature is configurable using the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadConfig config.simpleUpload} object.
import SimpleUploadAdapter from '@ckeditor/ckeditor5-upload/src/simpleuploadadapter';
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ SimpleUploadAdapter, ... ],
toolbar: [ ... ],
simpleUpload: {
// The URL the images are uploaded to.
uploadUrl: 'http://example.com',
// Headers sent along with the XMLHttpRequest to the upload server.
headers: {
'X-CSRF-TOKEN': 'CSFR-Token',
Authorization: 'Bearer <JSON Web Token>'
}
}
} )
.then( ... )
.catch( ... );
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.
When the image upload process is initiated, the adapter sends a POST request under {@link module:upload/adapters/simpleuploadadapter~SimpleUploadConfig#uploadUrl config.simpleUpload.uploadUrl}.
You can sent additional headers along with the XMLHttpRequest to the upload server, e.g. to authenticate the user, using the {@link module:upload/adapters/simpleuploadadapter~SimpleUploadConfig#uploadUrl config.simpleUpload.headers} object.
The responseType of the request is always json. See the "Successful upload" and "Error handling" sections to learn more.
If the upload is successful, the server should either return:
an object containing the url property which points out to the uploaded image on the server:
{
"url": "https://example.com/images/foo.jpg"
}
an object with the urls property, if you want to use responsive images and the server supports it:
{
"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 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.
URL(s) in the server response are used:
If something went wrong, the server must return an object that containing the error property. This will terminate the upload in the editor, e.g. 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 {@link module:ui/notification/notification~Notification#showWarning editor notification system} and displayed to the user. For the convenience of the users, use clear and possibly specific error messages.
{
"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 {@link module:ui/notification/notification~Notification#showWarning editor notification system} will display the default "Couldn't upload file: [filename]." message.
This upload adapter will notify users about the file upload progress out–of–the–box.
Check out the comprehensive {@link features/image-upload Image upload overview} to learn more about different ways of uploading images in CKEditor 5.
See the {@link features/image Image feature} guide to find out more about handling images in CKEditor 5.
The source code of the feature is available on GitHub in https://github.com/ckeditor/ckeditor5-upload.