Separate the file-byte limit from image decoding

●4
calendar_today ago • schedule1 min read

Give a file-size rejection its own result in an image-preview flow. You can check the selected file's byte budget before starting decoding, then report a size problem without calling it an image-decoding failure.

MDN documents File.size, inherited from Blob, as a byte count for the stored file. It is not the size of a decoded pixel buffer. This guard uses an example limit of 8 MiB; a file exactly at that limit passes.

function checkFileBudget(file, maxBytes) {
  if (!file) return { ok: false, reason: "no-file" };
  if (file.size > maxBytes) {
    return { ok: false, reason: "file-too-large" };
  }
  return { ok: true };
}

const budget = 8 * 1024 * 1024; // example policy: 8 MiB

Run the guard before starting preview work. When ok is false, stop and show its reason. Replace the example budget with the limit your endpoint actually requires, and state the unit in the message.

A passing result says only that the file fits this byte budget. The decoder can still fail. Handle that failure separately from an upload response that the server rejects. Enforce the upload requirements on the server as well.

The file input's accept attribute does not close that gap. As MDN explains, it guides the file picker rather than validating the chosen file's type. Selection through an image-oriented picker is not evidence that an upload is valid.

References:
https://developer.mozilla.org/en-US/docs/Web/API/Blob/size
https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/accept

I work on Morphio (https://morphio.online/). This example describes an image-handling check, without claiming that it is a particular product's implementation.

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Canvas cropping: keep source pixels separate from output size

jpgtosvg - Oct 17

Everyone says DeepSeek is cheaper, but I got tired of guessing the exact math. So I built a calculat

abarth23 - Apr 27

Cisco's Amy Chang: A Model's "Passport" Doesn't Tell You Where It Actually Came From

Tom Smithverified - Aug 27

A Message From Our Founder: On the Winding Down of LaaSy™

Ken W. Algerverified - Oct 29

Validate Image Enlargement at the Size Users Will Actually See

happyhorse-video - Oct 23
chevron_left
154 Points • 4 Badges
2Posts
0Comments
I work on Morphio, an online toolkit for HEIC conversion and image format workflows. I write about i... Show more

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!