首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ckEditors UploadAdapter未设置ImageSource

ckEditors UploadAdapter未设置ImageSource
EN

Stack Overflow用户
提问于 2020-10-15 20:59:54
回答 1查看 56关注 0票数 0

我在我的网站上使用ckEditor和CostumAploadAdapter。我想为我的WebSiteUsers创建一个电子邮件客户端。当我在ckEditor中插入图像时,图像通过UploadAdapter上传到服务器,并在编辑器中显示。但是,当我在服务器端查看发送的文本内容时,它只包含每个图像的"<figure class="image"><img></figure>"。而不是映像的名称或服务器上的位置。我尝试在适配器的加载侦听器中显式地设置imageName (仅用于测试目的),但没有成功。

有没有人知道为什么Upload Adapter没有插入正确的数据,或者我必须在哪里操作Upload Adapter,让它做它应该做的事情?

下面是我的uploadAdapter的代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?php

$uploaddir = "uploadImages\\";
$uploadfile = $uploaddir . ($_FILES['fileToUpload']['name']);

move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile);

echo "{\"url\":\"meer.jpg\"}";

?>
EN

回答 1

Stack Overflow用户

发布于 2020-10-24 05:56:59

首先验证文件是否上传

在你的文件中:$file_name =$_ uploadScript‘’fileToUpload‘;回显$file_name;

如果返回,则文件名表示文件已正确上传

我对此有一点怀疑

代码语言:javascript
复制
$uploaddir = "uploadImages\\";

请更改为

代码语言:javascript
复制
$uploaddir = "uploadImages/";

希望这能有所帮助

根据ckeditor文档,您的js是正确的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64372136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档