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

347
Views
How to write file size validation logic in React & Typescript

I am working with React & Typescript and want to add validation logic when uploading image files. It accepts up to 2 images and 10 Megabyte (sum of 2 images) is the maximum size that user can upload.

I currently have below code to validate the above logic but I am confused if my unit comparison is correct.

    const onFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
        const selectableMaxFileSize = 1024 * 1024 * 10 // 10 Megabyte
        let initialFileSize: number = 0

        if (e.target.files) {
         for (let i = 0; i < e.target.files.length; i++) {
                const file = e.target.files[i]
                initialFileSize += file.size
            }
        if (initialFileSize > selectableMaxFileSize) {
            setHasModal(true)
            e.target.value = ''
            return
        }

Also I noticed I can simply access to file size by e.target.size, then compare with selectableMaxFileSize - does this work as well?

about 4 years ago · Juan Pablo Isaza
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!