Untitled

 avatar
unknown
plain_text
2 years ago
844 B
4
Indexable
import React from 'react';

const ImageUploader = () => {
  const handleImageChange = (e) => {
    const files = e.target.files;
    const newImages = Array.from(files);

    // Check if each file exceeds 2MB
    const exceedsLimit = newImages.some(file => file.size > 2 * 1024 * 1024); // 2MB in bytes

    if (exceedsLimit) {
      alert('Error: File size exceeds 2MB limit.');
      return; // Do not process and store the files if any exceeds the limit
    }

    // Convert images to base64 strings
    const base64Images = newImages.map(image => URL.createObjectURL(image));

    // Store base64 strings in local storage
    localStorage.setItem('uploadedImages', JSON.stringify(base64Images));
  };

  return (
    <div>
      <input type="file" onChange={handleImageChange} multiple />
    </div>
  );
};

export default ImageUploader;
Editor is loading...
Leave a Comment