在CkEditor中获取所选文件并添加自定义上传按钮

问题描述 投票:0回答:1
  1. 我正在使用ngx-ckeditor:角度为5的“0.4.0”。
  2. 我想要上传图片并添加自定义上传按钮
  3. 下面是我的HTML。 <ck-editor #ckeditor name="html_template" [(ngModel)]="mailModel.html_template" [config]="ckEditorConfig"> </ck-editor>
  4. 这是我的组件代码。 this.ckEditorConfig = { filebrowserBrowseUrl : '/application/crm/distribution-list/create-mail', filebrowserUploadUrl : 'http://192.168.0.107:8000/api/crm/v1.0/crm-distribution-library-files', fileTools_requestHeaders :{ 'X-Requested-With': 'XMLHttpRequest', Authorization: 'Bearer ' + localStorage.getItem('access_token') }, filebrowserUploadMethod : 'xhr', removeButtons: 'Forms,Iframe,Blocks,Subscript,Superscript,Maximize,Undo', };
  5. 使用此代码,我无法获取图像,无法通过我的自定义标头。

我想获得所选图像并添加自定义的“上传图片”按钮。

angularjs ckeditor angular5
1个回答
1
投票

下面是在CkEditor中添加自定义按钮的代码

@ViewChild('ckeditor') ckeditor: CKEditorComponent;

ngAfterViewInit(): void {
    this._addImageUploadBtn();
}

_addImageUploadBtn() {
    const editor = this.ckeditor && this.ckeditor.instance;
    if (!editor) {
      return;
    }
    var that = this;
    editor.ui.addButton('uploadImage', {
        icon: 'https://img.icons8.com/ios/50/000000/image.png',
        label: 'Upload Image',
        command: 'uploadImage',
        toolbar: 'insert'
      });
    editor.addCommand('uploadImage', {
      exec: function(editor: any) {
        // Remove img input.
        [].slice.apply(document.querySelectorAll('.ck-editor-upload-img')).forEach((img: any) => {
          img.remove();
        });
        const input = document.createElement('input');
        input.setAttribute('type', 'file');
        input.setAttribute('class', 'ck-editor-upload-img');
        input.style.display = 'none';
        input.addEventListener('change', e => {
            const file = (e.target as HTMLInputElement).files[0];
            if (file) {
               console.log(file);
               // Do Stuff
            }
          },
          false
        );
        document.body.appendChild(input);
        input.click();
      }
    });
  }

在这里,您将获得所选的图像文件,并获得自定义按钮单击。

© www.soinside.com 2019 - 2024. All rights reserved.