Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saosangmo/f4884ca8e75cfe01541d2770ddbca014 to your computer and use it in GitHub Desktop.
Save saosangmo/f4884ca8e75cfe01541d2770ddbca014 to your computer and use it in GitHub Desktop.
CKEditor5 Image Upload.php
//php
if(isset($_FILES["upload"])){
$ext = ".".end((explode(".", $_FILES["upload"]["name"])));
$filename="test";
$file_public_addr =yourlocaladdr.$filename.$ext;
$success=move_uploaded_file($_FILES["upload"]["tmp_name"],SYS_EXT.$file_public_addr);
if( $success){
$json["uploaded"]=true;
$json["url"]=$localfileaddr;
json_encode($json);
}
}
if(!$success){
$json["uploaded"]=false;
$json["error"]=array("message"=>"Error Uploaded");
json_encode($json);
}
}
//html
<div id="editor">
<p>This is the editor content.</p>
</div>
<script src="/@ckeditor/ckeditor5-build-classic/build/ckeditor.js"></script>
<script src="/@ckeditor/ckeditor5-build-classic/build/myuploader.js"></script>
<script>
function MyCustomUploadAdapterPlugin( editor ) {
editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
return new MyUploadAdapter( loader );
};
}
ClassicEditor
.create( document.querySelector( '#editor' ),{
extraPlugins: [ MyCustomUploadAdapterPlugin ],
} )
.then( editor => {
window.editor = editor;
} )
.catch( err => {
console.error( err.stack );
} );
editor.ui.view.height='200px';
</script>
//javascript
class MyUploadAdapter {
constructor( loader ) {
// CKEditor 5's FileLoader instance.
this.loader = loader;
console.log(loader);
// URL where to send files.
this.url = '/upload/path';
}
// Starts the upload process.
upload() {
return new Promise( ( resolve, reject ) => {
this._initRequest();
this._initListeners( resolve, reject );
this._sendRequest();
} );
}
// Aborts the upload process.
abort() {
if ( this.xhr ) {
this.xhr.abort();
}
}
// Example implementation using XMLHttpRequest.
_initRequest() {
const xhr = this.xhr = new XMLHttpRequest();
xhr.open( 'POST', this.url, true );
xhr.responseType = 'json';
}
// Initializes XMLHttpRequest listeners.
_initListeners( resolve, reject ) {
const xhr = this.xhr;
const loader = this.loader;
const genericErrorText = 'Couldn\'t upload file:' + ` ${ loader.file.name }.`;
xhr.addEventListener( 'error', () => reject( genericErrorText ) );
xhr.addEventListener( 'abort', () => reject() );
xhr.addEventListener( 'load', () => {
const response = xhr.response;
if ( !response || response.error ) {
return reject( 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.
resolve( {
default: response.url
} );
} );
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() {
const data = new FormData();
data.append( 'upload', this.loader.file );
this.xhr.send( data );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment