首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在图像上传jodit编辑器中自定义url

在图像上传jodit编辑器中自定义url
EN

Stack Overflow用户
提问于 2020-01-03 18:16:19
回答 2查看 3.1K关注 0票数 0

我已经实现了Jodit编辑器(react)https://github.com/jodit/jodit-react,插入图像选项,其中您可以上传图像,这保存为默认选项的编辑器,

我想知道如何使用自定义网址和插入编辑器中的图像

Jodit默认行为

代码语言:javascript
运行
复制
config:{

 readonly: false,
        enableDragAndDropFileToEditor: true,        
        uploader: { url: "https://xdsoft.net/jodit/connector/index.php?action=fileUpload"}

}

期望如何添加自定义url

代码语言:javascript
运行
复制
config:{

 readonly: false,
        enableDragAndDropFileToEditor: true,        
        uploader: { url: "www.xyz.com/upload"}

}
EN

回答 2

Stack Overflow用户

发布于 2020-07-27 02:48:44

在这个存储库中,https://github.com/GilianMoa/jodit-editor-react我正在使用Cloudinary api上传图像。希望我能帮助你完成这段代码。

我创建了一个自定义按钮:

代码语言:javascript
运行
复制
uploadImageButton = () => {
        Jodit.defaultOptions.controls.uploadImage = {
            name: 'Upload image to Cloudinary',
            iconURL: "https://www.kindpng.com/picc/m/261-2619141_cage-clipart-victorian-cloud-upload-icon-svg-hd.png",
            exec: (async (editor) => {
                await this.imageUpload(editor);
            })
        };
    }

然后,我创建了一个方法来打开一个对话框,选择图像并将其发送到一个服务,该服务将一个包含图像文件formData的post发送到cloudinary。

代码语言:javascript
运行
复制
//dialog method
imageUpload = (editor) => {
    
            const input = document.createElement('input');
            input.setAttribute('type', 'file');
            input.setAttribute('accept', 'image/*');
            input.click();
    
            input.onchange = async function () {
    
                const imageFile = input.files[0];
    
                if (!imageFile) {
                    return;
                }
    
                if (!imageFile.name.match(/\.(jpg|jpeg|png)$/)) {
                    return;
                }
    
                const imageInfo = await FileUpload(imageFile);;
    
                this.insertImage(editor, imageInfo.url);
    
            }.bind(this);
        }

//this method insert the image inside the editor after the upload is done.
        insertImage = (editor, url) => {
            const image = editor.selection.j.createInside.element('img');
            image.setAttribute('src', url);
            editor.selection.insertNode(image);
        }




// this method send the image to cloudinary
    export const FileUpload = async (file) => {
        let result = null;
    
        let formData = new FormData();
        formData.append('file', file);
        formData.append('upload_preset', `${process.env.REACT_APP_CLOUDINARY_UPLOAD_PRESET}`);
    
        await fetch(`https://api.cloudinary.com/v1_1/${process.env.REACT_APP_CLOUDINARY_CLOUD_NAME}/image/upload`, {
            method: 'POST',
            body: formData
        })
            .then((response) => response.json())
            .then((data) => {
                result = data;
            })
            .catch((error) => {
                console.error('Error:', error);
            });
    
        return result;
    }
票数 1
EN

Stack Overflow用户

发布于 2020-09-23 10:52:05

我使用jodit-react上传img成功!代码在这里,希望能对你有所帮助。

代码语言:javascript
运行
复制
 <JoditEditor
                ref={this.editor}
                value={'this.state.content'}
                config={{
                  language: 'zh_cn',
                  toolbarButtonSize: 'large',
                  uploader: {
                    url: '/manage/upload',  //your upload api url
                    insertImageAsBase64URI: false, not inster base64
                    imagesExtensions: ['jpg', 'png', 'jpeg', 'gif'],
                    //headers: {"token":`${db.token}`},
                    filesVariableName: function (t) {
                      return 'files[' + t + ']';
                    }, //"files",
                    withCredentials: false,
                    pathVariableName: 'path',
                    format: 'json',
                    method: 'POST',
                    prepareData: function (formdata) {
                      return formdata;
                    },
                    isSuccess: function (e) {
                      debugger;
                      return e.success;
                    },
                    getMessage: function (e) {
                      return void 0 !== e.data.messages && Array.isArray(e.data.messages)
                        ? e.data.messages.join('')
                        : '';
                    },
                    process: function (resp: any) { //success callback transfrom data to defaultHandlerSuccess use.it's up to you.
                      let files = [];
                      files.unshift(resp.data);
                      return {
                        files: resp.data,
                        error: resp.msg,
                        msg: resp.msg,
                      };
                    },
                    error: function (this: any, e: Error) { 
                      this.j.e.fire('errorMessage', e.message, 'error', 4000);
                    },
                    defaultHandlerSuccess: function (this: Uploader, resp: IUploaderData) { // `this` is the editor.
                      const j = this;
                      debugger;
                      if (resp.files && resp.files.length) {
                        const tagName = 'img';
                        resp.files.forEach((filename: string, index: number) => { //edetor insertimg function
                          const elm = j.createInside.element(tagName);
                          elm.setAttribute('src', filename);
                          j.s.insertImage(elm as HTMLImageElement, null, j.o.imageDefaultWidth);
                        });
                      }
                    },
                    defaultHandlerError: function (this: any, e) {
                      this.j.e.fire('errorMessage', e.message);
                    },
                    contentType: function (e) {
                      return (
                        (void 0 === this.jodit.ownerWindow.FormData || 'string' == typeof e) &&
                        'application/x-www-form-urlencoded; charset=UTF-8'
                      );
                    },
                  },
                }}
              />

我已经测试好了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59576741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档