Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.3k stars 160 forks source link

How can i use in for multiple file upload? #59

Closed phukol closed 4 years ago

phukol commented 4 years ago

Thanks for this wonderful library. Just want to ask if how I can use it for a file input with multiple images attached? Thank you.

Donaldcwl commented 4 years ago

You may try the following code snippet:

async function handleImageUpload(event) {

  const imageFiles = Array.from(event.target.files);

  const options = {
    maxSizeMB: 1,
    maxWidthOrHeight: 1920,
    useWebWorker: true
  }
  try {
    const compressedFiles = await Promise.all(
        imageFiles.map(imageFile => await imageCompression(imageFile, options))
    )

    await uploadToServer(compressedFiles); // write your own logic
  } catch (error) {
    console.log(error);
  }

}