tinymce上传图片报错:图像上传失败:Cannot read properties of undefined (reading 'then')

1、我在之前的版本时这样上传图片的,之前的版本上传没有问题。

images_upload_handler: function(blobInfo, success, failure){
                var xhr, formData;
                xhr = new XMLHttpRequest();
                xhr.withCredentials = false;
                console.log("blob = "+blobInfo.blob());
                const url = jConstant.SERVICE_URL_PREFIX + 'fileController/uploadImage'
                xhr.open("POST", url);
                //xhr.setRequestHeader("Content-Type","multipart/form-data");
                //xhr.setRequestHeader("Authorization","Basic anVpY2VJdFVzZXI6anVpY2VJdFVzZXIyMDIy");
                formData = new FormData();
                formData.append("file", blobInfo.blob());
                xhr.onload = function(e){
                    var json;
                    if (xhr.status != 200) {
                        failure('HTTP Error: ' + xhr.status);
                        return;
                    }
                    json = JSON.parse(this.responseText);
                    if (!json || typeof json.location != 'string') {
                        failure('Invalid JSON: ' + xhr.responseText);
                        return;
                    }
                    success(json.location);
                };
                xhr.send(formData);
            }

  在新的tinymce版本,用这个就报错

最后改成官网的示例就可以了

images_upload_handler: (blobInfo, progress) => new Promise((resolve, reject) => {
            const xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
            xhr.open('POST', url);
            xhr.upload.onprogress = (e) => {
              progress(e.loaded / e.total * 100);
            };
            xhr.onload = () => {
              if (xhr.status === 403) {
                reject({ message: 'HTTP Error: ' + xhr.status, remove: true });
                return;
              }
              if (xhr.status < 200 || xhr.status >= 300) {
                reject('HTTP Error: ' + xhr.status);
                return;
              }
              const json = JSON.parse(xhr.responseText);
              if (!json || typeof json.location != 'string') {
                reject('Invalid JSON: ' + xhr.responseText);
                return;
              }
              resolve(json.location);
            };
            xhr.onerror = () => {
              reject('Image upload failed due to a XHR Transport error. Code: ' + xhr.status);
            };
          
            const formData = new FormData();
            formData.append('file', blobInfo.blob(), blobInfo.filename());
          
            xhr.send(formData);
        })

  

 

posted @ 2024-08-25 18:59  信铁寒胜  阅读(1)  评论(0编辑  收藏  举报