Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Unhandled Rejection (Error): The file given is not an image
  • here i am upload a file using formik in react js

  • When the file is upload it's size validation works but type validation not work

  • Here is my code If anyone can help me so it's really helpful to me

     const SUPPORTED_FORMATS = ["image/jpg", "image/jpeg","image/png"];
    
    const onFileChange = async (file) => { 
     const options = {maxSizeMB: 1, maxWidthOrHeight: 500, useWebWorker: true} 
    
     const compressedFile =await imageCompression(file, options)
       console.log(`------- Compressed File Size ${compressedFile.size / 1024} KB -------`)
    
       var formData = new FormData()
       formData.append('file', compressedFile)
       var url = URL.BASE + URL.STORAGE_UPLOAD + 'profileImage'
      var result = await post(url, formData, 'multipart/form-data')
    
      if (result.is_success === false) {
          addNotification(result.error_message, 'danger');
        }
    
       else{      
       var url= reader.readAsDataURL(file)
       reader.readAsDataURL(file)
    
        reader.onloadend = function (e) {
        setSelectedImgUrl(reader.result);
          setProfileImage(result.data.fileName);    
       }
       .bind(this)
    } }
    
    
    
      profileImgUrl: Yup
             .mixed()
            .nullable() 
            .notRequired()      
             .test(
                     "filesize",
                     requiredMessages.file_size_validation,
                      (file) => !file || (file && file.size <= 2000000)
                  )      
             .test(
                     "fileformat",
                     requiredMessages.file_type_validation,
                     (file) => !file || (file && SUPPORTED_FORMATS.includes(file.type))
                 ),
    
    
    
      <input className="fileInput hidden" type="file"  name="profileImgUrl"  id="profileImgUrl" accept="image/*  onBlur={handleBlur} onChange={(event) => { setFieldValue("profileImgUrl",event.target.files[0])onFileChange(event.target.files[0])}}/>`
    
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You set the Max Size on the options declaration. You did not set the allowed Types.

 const options = {maxSizeMB: 1, maxWidthOrHeight: 500, useWebWorker: true} 

Try this (would double check if it's .jpg or jpg as I can't recall the syntax):

const filesFormats = [".jpg", ".gif"];
const isRightFormat = filesFormats.includes(file.type);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!