簡體   English   中英

VueJS CKeditor5 上傳圖片

[英]VueJS CKeditor5 upload images

在 Vuejs 中使用 CKeditor5 上傳圖片時遇到問題。

首先嘗試了簡單上傳適配器,它給了我以下錯誤:

原因:CKEditorError: ckeditor-duplicated-modules: 某些 CKEditor 5 模塊重復。 閱讀更多: https : //ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-ckeditor-duplicated-modules

我嘗試制作上傳適配器。 作為上傳適配器,我拿了這個例子並修改了 url。 uploadadapter.js 文件如下所示:

    export default class UploadAdapter {
        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();
    
            xhr.open( 'POST', '<url here>', 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;
    
                if ( !response || response.error ) {
                    return reject( response && response.error ? response.error.message : genericErrorText );
                }
   
                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( file ) {
            // Prepare the form data.
            const data = new FormData();
    
            data.append( 'upload', file );
    
            // Send the request.
            this.xhr.send( data );
        }
    }

Vue 組件:

    <template>
        <form @submit.prevent="store">
            <ckeditor
                :editor="editor"
                v-model="form.content"
                :error-messages="errors.content"
                :config="editorConfig"
            />
        </form>
    </template>
    
    <script>
        import CKEditor from '@ckeditor/ckeditor5-vue';
        import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
        import UploadAdapter from '../../UploadAdapter';
    
        export default {
            data()
            {
                return {
                    form: {
                        content: null,
                    },
                    editor: ClassicEditor,
                    editorConfig: {
                        toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', '|', 'insertTable', '|', 'imageUpload', 'mediaEmbed', '|', 'undo', 'redo' ],
                        table: {
                            toolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
                        },
                        extraPlugin: [this.uploader],
                        language: 'nl',
                    },
                }
            },
    
            methods: {
                store()
                {
                    // Some code
                },
    
                uploader(editor)
                {
                    editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
                        return new UploadAdapter( loader );
                    };
                },
            },
    
            components: {
                ckeditor: CKEditor.component
            }
        }
    </script>

但是,每次嘗試上傳文件時,都會返回以下警告:

filerepository-no-upload-adapter:未定義上傳適配器。 閱讀更多: https : //ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-filerepository-no-upload-adapter

看過網址,但它只是讓我轉圈,因此沒有任何進展。 我正在尋找的是一個至少將文件發送到服務器而沒有錯誤/警告的示例。 如果uploadadapter可以刮掉和別的東西,除了CKfinder可以使用的罰款。 現在我想問題很可能出在 Vue 組件中。

使用extraPlugins而不是extraPlugin

將上傳器功能移到 vue 組件之外,然后直接將其用作

extraPlugin: [uploader]

為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM