Angular 带有自定义上载适配器的CKEditor 5上载图像

Angular 带有自定义上载适配器的CKEditor 5上载图像,angular,ckeditor,ckeditor5,Angular,Ckeditor,Ckeditor5,我正在用Angular 11实现CKEditor 5,它有一个Spring引导后端。 我实现了一个自定义图像上载适配器,如所示 在后端,我使用令牌实现了安全性 上传工作正常。然而,图像的加载不起作用,它给出了HTTP 401错误 如何自定义自定义适配器以加载带有令牌的图像 代码如下: export class CustomUploadAdapter { public loader: any; public url: string; public xhr: XMLHttpR

我正在用Angular 11实现CKEditor 5,它有一个Spring引导后端。 我实现了一个自定义图像上载适配器,如所示

在后端,我使用令牌实现了安全性

上传工作正常。然而,图像的加载不起作用,它给出了HTTP 401错误

如何自定义自定义适配器以加载带有令牌的图像

代码如下:

export class CustomUploadAdapter {
    public loader: any;
    public url: string;
    public xhr: XMLHttpRequest;
    public token: string;

    constructor(
        loader    
    ) {
        this.loader = loader;
    }

    upload() {

        return new Promise(async (resolve, reject) => {
            this.loader.file.then((file) => {
                this._initRequest();
                this._initListeners(resolve, reject, file);
                this._sendRequest(file);
            });
        });
    }

    abort() {
        if (this.xhr) {
            this.xhr.abort();
        }
    }

    _initRequest() {
        const xhr = (this.xhr = new XMLHttpRequest());
        xhr.open("POST", this.url, true);
        this.token = localStorage.getItem("access_token");
        this.token = localStorage.getItem("access_token");
        
        xhr.setRequestHeader("Authorization", 'Bearer ' + this.token);

        xhr.responseType = "json";
    }

    _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(
                response.urls
            );
        });

        if (xhr.upload) {
            xhr.upload.addEventListener("progress", (evt) => {
                if (evt.lengthComputable) {
                    loader.uploadTotal = evt.total;
                    loader.uploaded = evt.loaded;
                }
            });
        }
    }

    _sendRequest(file) {
        const data = new FormData();

        data.append("file", file);

        this.xhr.send(data);
    }
}

export class Component implements OnInit {

  @ViewChild('ckeditor', { static: false })
  ckeditor: any;

  public configCkeditor = Ckeditor5Util.configCkeditor; //configs...
   ...
   public onReady(editor) {
    editor.plugins.get("FileRepository").createUploadAdapter = (loader) => {
      return new CustomUploadAdapter(loader);
    };
    ...
   }
   ...
}

html {
...
<ckeditor [editor]="editor" (ready)="onReady($event)" formControlName="texto" debounce="500"
      [config]="configCkeditor"></ckeditor>
...
}
导出类CustomUploadAdapter{
公共装载机:任何;
公共url:string;
公共xhr:XMLHttpRequest;
公共令牌:字符串;
建造师(
装载机
) {
this.loader=loader;
}
上传(){
返回新承诺(异步(解析、拒绝)=>{
this.loader.file.then((文件)=>{
这个._initRequest();
此._initListeners(解析、拒绝、文件);
此._sendRequest(文件);
});
});
}
中止{
if(this.xhr){
this.xhr.abort();
}
}
_initRequest(){
const xhr=(this.xhr=new XMLHttpRequest());
打开(“POST”,this.url,true);
this.token=localStorage.getItem(“访问令牌”);
this.token=localStorage.getItem(“访问令牌”);
xhr.setRequestHeader(“授权”,“承载人”+此.token);
xhr.responseType=“json”;
}
_initListeners(解析、拒绝、文件){
const xhr=this.xhr;
const loader=this.loader;
const genericErrorText=“无法上载文件:”+`${file.name}.`;
xhr.addEventListener(“错误”,()=>reject(genericErrorText));
addEventListener(“中止”,()=>reject());
xhr.addEventListener(“加载”,()=>{
常量响应=xhr.response;
如果(!response | | response.error){
退货拒绝(
response&&response.error?response.error.message:genericErrorText
);
}
决心(
response.url
);
});
if(xhr.upload){
xhr.upload.addEventListener(“进度”,evt)=>{
if(evt.长度可计算){
loader.uploadTotal=evt.total;
loader.upload=evt.loaded;
}
});
}
}
_发送请求(文件){
const data=新表单数据();
数据。追加(“文件”,文件);
此.xhr.send(数据);
}
}
导出类组件实现OnInit{
@ViewChild('ckeditor',{static:false})
编辑:任何;
public configCkeditor=Ckeditor5Util.configCkeditor;//配置。。。
...
公共onReady(编辑器){
editor.plugins.get(“FileRepository”).createUploadAdapter=(loader)=>{
返回新的CustomUploadAdapter(加载器);
};
...
}
...
}
html{
...
...
}