添加自定义 headers 以上传图片

Add custom headers to upload image

我目前正在尝试将 CKeditor 5 ReactComponent 集成到我的应用程序中。

我遇到上传图片功能的问题...我使用 Node/Express 后端,它使用 JWT 身份验证中间件,因此每个请求都必须有一个 Authorization header 为了通过。

我想知道是否可能出现以下情况之一:

下面是我的代码

<CKEditor
  editor={ClassicEditor}
  data="<p>Add product description here</p>"
  onInit={(editor) => {
    // You can store the "editor" and use when it is needed.
    //console.log('Editor is ready to use!', editor);
  }}
  onChange={(event, editor) => {
    const data = editor.getData();
    this.handleData(data)
  }}
  config={{
    ckfinder: {
      uploadUrl: `${apiUrl}/upload/images/description`,
    },
  }}
/>

谢谢

在 属性 onInit

中用这段代码试试
onInit={ editor => {
 editor.plugins.get( 'FileRepository' ).createUploadAdapter = function( loader ) {
  return new UploadAdapter( loader );
 };
}}

在您必须创建 class UploadAdapter

之后
class UploadAdapter {
  constructor( loader ) {
    // Save Loader instance to update upload progress.
    this.loader = loader;
  }

  upload() {
    const data = new FormData();
    data.append('typeOption', 'upload_image');
    data.append('file', this.loader.file);

    return new Promise((resolve, reject) => {
      axios({
        url: `${API}forums`,
        method: 'post',
        data,
        headers: {
          'Authorization': tokenCopyPaste()
        },
        withCredentials: true
      }).then(res => {
        console.log(res)
        var resData = res.data;
        resData.default = resData.url;
        resolve(resData);
      }).catch(error => {
        console.log(error)
        reject(error)
      });
    });
  }

  abort() {
    // Reject promise returned from upload() method.
  }
}