0

I am working on a project where I have one page where you should be able to add images. One by one it's worked perfectly. Now I want to make to be able to multiple add and upload. I don't want to use any library for image upload.

  const onFileChange = (e) => {
    const reader = new FileReader();
    reader.onload = () => {
      if (reader.readyState === 2) {
        setPreview(reader.result);
      }
    };
    reader.readAsDataURL(e.target.files[0]);
    if (e.target.files[0]) {
      setOver(true);
    }

    const copy = [...image];
    copy.push(e.target.files[0]);
    setImage([...copy]);
  };
  const onFileUpload = () => {
    const formdata = new FormData();
    image.forEach((elem) => {
      formdata.append("data", elem);
    });
    formdata.append("id_grobnog_mjesta", Id);

    addImage(formdata);
  };

  const addImage = async (data) => {
    try {
      setIsLoading(true);
      const response = await apiRequest({
        method: "post",
        url: `spisak-srebrenica/upload`,
        headers: {
          Authorization: `Bearer ${token}`,
        },
        data,
      });
      if (response.data.success) {
        getVictimImage();
      }
      setIsLoading(false);
      setImage([]);
    } catch (err) {
      setIsLoading(false);
      setImage([]);
    }
  };

Upload component :

 <Upload>
      <BiImageAdd size={50} opacity={0.5} />
      <input type="file" onChange={onFileChange} />
      <div className="items">
        <p>Dodajte sliku</p>
        <span className="format">PNG,JPG,GIF do 10MB</span>
      </div>
    </Upload>
bajrax
  • 47
  • 3

1 Answers1

0

add multiple attribute

<input type="file" id="files" name="files" multiple>

https://www.w3schools.com/tags/att_input_multiple.asp

Avani Bataviya
  • 750
  • 1
  • 6
  • 20