【发布时间】:2021-01-29 23:18:21
【问题描述】:
我在我的网站上使用 ckEditor 和 CostumApploadAdapter。
我想为我的网站用户创建某种电子邮件客户端。
当我在我的 ckEditor 中插入图像时,图像会通过 UploadAdapter 上传到服务器,并且图像会显示在编辑器中。
但是当我在服务器端查看发送文本的内容时,它只包含每个图像的"<figure class="image"><img></figure>"。不是图像的名称或服务器上的位置。
我尝试在适配器的加载侦听器中显式设置 imageName(仅用于测试目的),但没有成功。
是否有人知道为什么上传适配器没有插入正确的数据,或者我必须在哪里操作上传适配器才能完成它应该做的事情?
这是我的 uploadAdapter 的代码:
class MyUploadAdapter {
constructor( loader ) {
// The file loader instance to use during the upload.
this.loader = loader;
}
// Starts the upload process.
upload() {
return this.loader.file
.then( file => new Promise( ( resolve, reject ) => {
this._initRequest();
this._initListeners( resolve, reject, file );
this._sendRequest( file );
} ) );
}
// 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 = new XMLHttpRequest();
// 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', '/uploadScript.php', true );
xhr.responseType = 'json';
}
// Initializes XMLHttpRequest listeners.
_initListeners( resolve, reject, file ) {
const xhr = this.xhr;
const loader = this.loader;
const genericErrorText = `Couldn't upload file: ${ file.name }.`;
xhr.addEventListener( 'error', () => reject( genericErrorText ) );
xhr.addEventListener( 'abort', () => reject() );
xhr.addEventListener( 'load', () => {
const response = xhr.response.url;
// 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 ) {
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.
// This URL will be used to display the image in the content. Learn more in the
// UploadAdapter#upload documentation.
resolve( {
default: response
} );
} );
// 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 = new FormData();
data.append( 'fileToUpload', 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 );
}
}
function MyCustomUploadAdapterPlugin( editor ) {
editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
// Configure the URL to the upload script in your back-end here!
return new MyUploadAdapter( loader );
};
}
ClassicEditor
.create( document.querySelector( '#message' ), {
extraPlugins: [ MyCustomUploadAdapterPlugin ]
} )
.catch( error => {
console.log( error );
} );
这里是php中的uploadScript:
<?php
$uploaddir = "uploadImages\\";
$uploadfile = $uploaddir . ($_FILES['fileToUpload']['name']);
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile);
echo "{\"url\":\"meer.jpg\"}";
?>
【问题讨论】:
标签: ajax xmlhttprequest ckeditor5