react-draft-wysiwyg icon indicating copy to clipboard operation
react-draft-wysiwyg copied to clipboard

how can I get the path of the image which I upload from local

Open niaofei2008 opened this issue 8 years ago • 8 comments

export default function uploadCallback(file) { return new Promise( (resolve, reject) => { resolve({ data: { link: "http://dummy_image_src.com" } }); } ); } <Editor toolbar={{ image: { uploadCallback: this.uploadCallback }}}} />

niaofei2008 avatar Feb 10 '17 09:02 niaofei2008

@niaofei2008: you need to provide your own api to editor for image upload and plug it in using a callback.

jpuri avatar Feb 10 '17 17:02 jpuri

uploadCallback(file):the param file has local path of image which i upload?

niaofei2008 avatar Feb 13 '17 01:02 niaofei2008

@jpuri 0bc56a30-3031-4f04-a653-4556a2691f72 upload local image ,the "src" is undefinde

niaofei2008 avatar Feb 13 '17 01:02 niaofei2008

screen shot 2017-02-13 at 8 48 34 am

Works for me, what kind of image are you uploading ?

jpuri avatar Feb 13 '17 03:02 jpuri

@jpuri chose one picture from my computer,src=undefined

niaofei2008 avatar Feb 13 '17 05:02 niaofei2008

I have similar problem with uploaded images, src attribute is unknown after I save content of Editor as html to db and then open it back. I found that this issue happens only if I return relative image path from my promise. If API returns absolute path, everything works as expected.

I believe this is not expected behaviour and relative paths should work as well.

This is how I retrieve html from editorState:

getHtml(editorState) {
    const rawContentState = convertToRaw( editorState.getCurrentContent() );
    const html = draftToHtml(rawContentState)
    return html;
  }

This is how I set content from db to editor:

componentWillReceiveProps(nextProps) {
    const html = nextProps.content;
    const blocksFromHtml = htmlToDraft(html);
    const state = ContentState.createFromBlockArray( blocksFromHtml.contentBlocks );
    this.setState({
      editor: EditorState.createWithContent(state),
    });
  }

This is my uploadCallback:

uploadImageCallBack(file) {
    const form = new FormData();
    form.append('photo', file);
    const url = Helpers.getServerUrl() + '/upload';
    return axios.post(url, form, {
        headers: {
          'Authorization': 'Bearer ' + localStorage.getItem('id_token'),
        },
      }
    )
    .then(response => {
      return {
        data: {
          link: '/uploads/' + response.data
        }
      }
    });
  }

ondrejrohon avatar Feb 23 '17 18:02 ondrejrohon

@jpuri @ondrejrohon How can i deal with this issue? If uploadCallback returns the relative paths, it does works! I think relative paths is necessary. Please help!

nanke- avatar Apr 11 '17 11:04 nanke-

No more news on that one? I'm facing the exact same problem :(

EDIT: never mind, all is in the js file object properties. Here is how to retrieve the image data (from the file passed to the uploadCallback callback):

const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => { // reader.result contains the base64 data of the user selected image };
reader.onerror = error => { ... };

MamorukunBE avatar Mar 28 '22 09:03 MamorukunBE