0

I'm working on a simple bulletin board with React-Quill. I'm implementing to communicate and save the content value to the server, but when uploading an image, there is an error that the image is not displayed in the content, so I can't solve it.

How can I solve this?

function Post() {

  const   QuillRef = useRef<ReactQuill>()
  const [Title, setTitle] = useState("")  
  const [contents, setcontents] = useState("")    

  const imageHandler = () => {
    const input = document.createElement("input");
    const formData = new FormData();


    input.setAttribute("type", "file");
    input.setAttribute("accept", "image/*");
    input.click();

    input.onchange = async () => {
      const file : any = input.files;
      if (file !== null) {
        formData.append("image", file[0]);

        console.log(formData)
      }
    }
  }
  const titleHandler = (e : React.ChangeEvent<HTMLTextAreaElement>) => {
      e.preventDefault()
      setTitle(e.currentTarget.value)
  }


  return (
    <div className="post">
        <ReactQuill
        style = {{height : "650px"}}
          ref = {(element) => {
            if(element != null) {
              QuillRef.current = element
            }
          }}
          value = {contents || ""}
          onChange = {(content, delta, source, editor) => setcontents(editor.getHTML())}
          modules = {modules}
          formats = {formats}
          theme = "snow"
          placeholder = "내용을 입력해주세요"/>
    </div>
  );
}

export default Post;

If you check FormData with Console.log, it comes out as an empty object.

sunpl13
  • 147
  • 4
  • 13
  • What about input.files? is that also coming empty? – Sanish Joseph Sep 01 '21 at 02:05
  • @SanishJoseph When I checked input.files, I saw that the value was coming in. However, the picture is not displayed inside the Content. – sunpl13 Sep 01 '21 at 04:00
  • Where and how are you passing the Formdata to the content? Share that part of the code . – Sanish Joseph Sep 01 '21 at 04:06
  • @SanishJoseph setcontents(editor.getHTML())}/> this part?? – sunpl13 Sep 01 '21 at 04:31
  • What I meant Is, aren't you missing `handlers: { image: imageHandler } ` . Your imageHandler is not used anywhere.. – Sanish Joseph Sep 01 '21 at 05:07
  • @SanishJoseph It has already been implemented as modules. `const modules = useMemo( () => ({ toolbar: { container: [ ["bold", "italic", "underline", "strike", "blockquote"], [{ size: ["small", false, "large", "huge"] }, { color: [] }], ["image", "video"], ], handlers: { image: imageHandler, }, }, }), [] );` – sunpl13 Sep 01 '21 at 06:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236634/discussion-between-sunpl13-and-sanish-joseph). – sunpl13 Sep 01 '21 at 06:28

0 Answers0