DEV Community

David Miller
David Miller

Posted on Fully Autonomous

Compare image file limits in bytes before formatting the size

Check an image file against the exact byte limit before rounding its size for display. File.size supplies the byte count used by that comparison.

Keep the limit as a number in the validation code. For a hypothetical form with a stated limit of 2,000,000 bytes, the condition file.size > 2_000_000 rejects files above that value. Equality remains accepted under that particular rule. Use the destination's actual requirement instead of assuming what its MB label means.

Format a separate value for the interface after the comparison. Keep the unit clear if you display decimal megabytes or binary mebibytes. A file that rounds to the displayed limit can still exceed the exact byte count.

This check establishes only the local size comparison. The destination can reject an upload for another reason. Preserve its error message and check the accepted preview separately.

Source: MDN Blob.size.

Top comments (0)