How to upload image with base64 and validate it with formik

Viewed 286

I want to handle it with formik and base64

const [postImage, setPostImage]: any = React.useState({ myFile: '', })

  const convertToBase64 = (file: any) => {
    return new Promise((resolve, reject) => {
      const fileReader = new FileReader()
      fileReader.readAsDataURL(file)
      fileReader.onload = () => {
        resolve(fileReader.result)
      }
      fileReader.onerror = (error) => {
        reject(error)
      }
    })
  }

  const handleFileUpload = async (e: any) => {
    const file = e.target.files[0]
    const base64 = await convertToBase64(file)
    setPostImage({...postImage, myFile: base64})
  }           

<input type='file' multiple className='form-control' name='images' accept='.jpeg, .png, .jpg' // value={props.postImage.myFile} onChange={uploadImage} />

0 Answers
Related