0

Currently the code for size restriction is the following. My image size in the network and on my desktop clearly says 5.1mb, however, the log of e.target.files[0].size is 5068146, which is 4.83mb and therefore doesn't display error. I am confused about why it doesn't display an error and why the check passes. I checked other stackoverflow and interent posts and it seems that my code is correct, so I don't know where the issue is and running out of ideas. For instance: https://www.geeksforgeeks.org/validation-of-file-size-while-uploading-using-javascript-jquery/

<input
            style={{ display: 'none' }}
            type="file"
            onChange={async e => {
              if (e.target.files[0].size > 5 * 1024 * 1024) {
                setImageSizeError('failure')
                return
              } else {
                setImageSizeError('success')
              }
              setLoading(true)
              const updatedStep = await fileUploader(e, stepId)
              setLoading(false)
              updateStep(updatedStep)
            }}
            accept=".jpg,.jpeg,.png,.pdf"
          />
pau
  • 88
  • 6
  • 2
    `5.068146` as a number, would be rounded to `5.1`, that's why. `kilo`, `mega` and the like are used in `1000^x` sense recently, and `1024^x` is referred as `kibi`, `mebi` (bi for binary) abominations. https://en.wikipedia.org/wiki/Byte#Multiple-byte_units – tevemadar Jan 16 '23 at 13:49
  • 1
    Does this answer your question? [Do you use "kibibyte" as a unit of measurement in your programs?](https://stackoverflow.com/questions/176922/do-you-use-kibibyte-as-a-unit-of-measurement-in-your-programs) – tevemadar Jan 16 '23 at 13:53

0 Answers0