{ ...editorConfig, simpleUpload: { // The URL that the images are uploaded to. uploadUrl: '/Upload/Image', // // Headers sent along with the XMLHttpRequest to the upload server. headers: { 'X-CSRF-TOKEN': 'CSFR-Token', Authorization: 'Bearer <JSON Web Token>' } } }
// Aborts the upload process. abort() { if (this.xhr) { this.xhr.abort(); } }
// Initializes the XMLHttpRequest object using the URL passed to the constructor. _initRequest() { const xhr = (this.xhr = newXMLHttpRequest());
// Note that your request may look different. It is up to you and your editor // integration to choose the right communication channel. This example uses // a POST request with JSON as a data structure but your configuration // could be different. xhr.open("POST", "http://example.com/image/upload/path", true); xhr.responseType = "json"; }
// This example assumes the XHR server's "response" object will come with // an "error" which has its own "message" that can be passed to reject() // in the upload promise. // // Your integration may handle upload errors in a different way so make sure // it is done properly. The reject() function must be called when the upload fails. if (!response || response.error) { returnreject(response && response.error ? response.error.message : genericErrorText); }
// If the upload is successful, resolve the upload promise with an object containing // at least the "default" URL, pointing to the image on the server. // This URL will be used to display the image in the content. Learn more in the // UploadAdapter#upload documentation. resolve({ default: response.url, }); });
// Upload progress when it is supported. The file loader has the #uploadTotal and #uploaded // properties which are used e.g. to display the upload progress bar in the editor // user interface. if (xhr.upload) { xhr.upload.addEventListener("progress", evt => { if (evt.lengthComputable) { loader.uploadTotal = evt.total; loader.uploaded = evt.loaded; } }); } }
// Prepares the data and sends the request. _sendRequest(file) { // Prepare the form data. const data = newFormData();
data.append("upload", file);
// Important note: This is the right place to implement security mechanisms // like authentication and CSRF protection. For instance, you can use // XMLHttpRequest.setRequestHeader() to set the request headers containing // the CSRF token generated earlier by your application.
// Send the request. this.xhr.send(data); } }
// ...
functionMyCustomUploadAdapterPlugin(editor) { editor.plugins.get("FileRepository").createUploadAdapter = loader => { // Configure the URL to the upload script in your back-end here! returnnewMyUploadAdapter(loader); }; }