skip to content

In a real-estate listing portal's web uploader, which checks and previews should run before any byte is sent, and what do they cost the page?

level: middleimportance: should knowfreq 42%

answer

  1. checks are feedback, the server decides
  2. cheapest first: count, size, type
  3. a type label can be empty or wrong
  4. object URL, not a data URL
  5. revoke every URL you create

basics

~20 s

Check count, size, type and, for photos, decoded dimensions before sending, to spare the agent's uplink, as feedback only because the server checks again. Preview with object URLs or small thumbnails, and revoke each URL when its tile goes.

solid answer

~50 s

Run the free checks first: the number of files against the listing's limit, each `File`'s `size`, and its `type`, which is only the browser's label and can be empty, so treat it as a hint. Then the paid one: decode each photo to read its dimensions and reject shots too small for a listing, one or two at a time so 80 decodes do not land together. Report a reason per file and start the accepted ones; never reject the batch. These checks are user experience, not security: anyone can send bytes without running the page, so the server repeats them. For previews, use `URL.createObjectURL(file)` rather than reading files into data URLs, but remember that showing a 12-megapixel photo in a small tile can still decode it at full size, so build small thumbnails and draw only visible tiles. Every object URL must be released with `URL.revokeObjectURL` when its tile is removed.

go deeper

for a junior

Recall which facts a File exposes for free (name, size, type, last-modified time) and that a preview comes from an object URL that must be released later.

for a middle

Explain why checks are feedback rather than security, why the type label is only a hint, why decoding costs memory, and when each object URL is revoked.

for a senior

Show the memory judgment for 80 phone photos: free checks before decodes, a small decode queue, resized thumbnails and visible-only tiles, and revocation tied to each tile's life.

for a principal

Decide which rules live in both places and how they stay in step: the server owns the truth, so publish the limits from the API and let the page read them instead of hard-coding copies.

## Why the page checks at all An agent selects 80 photos and a walkthrough video for a listing, often on a phone in the property. Anything the page can reject before sending saves minutes of a weak uplink and gives an answer in milliseconds instead of after an upload. That is the whole purpose: **client checks are user experience, not a security control**. A crafted request can reach the API or the storage URL without running the page's code at all, so the server repeats every rule; validation as a defence belongs to the questions on input validation. The file input's `accept` filter only narrows what the picker offers, so the page checks again after selection. ## The checks, cheapest first | Check | How the page knows | Cost | On failure | |---|---|---|---| | Count per listing (say, 80 photos, 1 video) | length of the selection | free | accept up to the limit, explain the rest | | Size per file | `File.size` | free | per-file message with the limit | | Type | `File.type`, plus the name's extension | free | a hint only; an empty or odd label is flagged, not trusted | | Duplicates | name, size and last-modified time | free | skip, with a note | | Photo dimensions | decode the image | CPU and memory | "too small for a listing photo" | Three details separate a good answer: - **`File.type` is a label, not proof.** It is the empty string when the browser cannot tell, and it says nothing certain about the bytes. Decoding the image is the stronger check the page can run, and the server still has the final word. - **Decoding is the expensive check.** Reading dimensions means decoding the image. A decoded 12-megapixel photo is about 12 million pixels times 4 bytes, roughly 48 MB, however small the file was. Run these checks in a small queue, one or two at a time, never 80 at once. - **Order matters.** Free checks first, so a 30 MB panorama or a document picked by mistake is rejected without paying for a decode. A hash of each photo is not needed to spot the common duplicate, the same photo picked twice: name, size and last-modified time catch it for free. If a design does hash files, the cost is the concern, and it belongs to the question on processing before upload. ## Previews without exhausting memory 1. **Use object URLs, not data URLs.** `URL.createObjectURL(file)` returns a short string that refers to the file. Reading the file into a data URL instead builds a base64 string about a third larger than the file, held in JavaScript memory: around 320 MB of strings for 80 photos of 3 MB each. 2. **Make small thumbnails.** An `<img>` pointed at the object URL of a full-size photo can still make the browser decode the full image, even for a 120-pixel tile. `createImageBitmap` accepts `resizeWidth` and `resizeHeight`, so the page can produce a small bitmap per photo, one at a time, and draw that instead. 3. **Draw only what is visible.** With 80 tiles, render or decode a tile only when it scrolls into view. 4. **Video:** a `<video>` element pointed at the walkthrough's object URL can show a frame and the duration without sending a byte. ## Releasing what you created - Each call to `URL.createObjectURL` creates a new URL, even for a file that already has one, and each must be released with `URL.revokeObjectURL`. - Object URLs are released automatically when the document unloads, but an uploader page can stay open for an hour while agents add and remove files, so waiting for unload is a leak. - Revoke when a tile is removed, when a file is cancelled, or when the server's own thumbnail replaces the local preview. - Do not revoke the moment the image loads if the tile is still on screen: the image stops being usable for actions such as opening it in a new tab. ## What the agent sees - Accepted files appear at once with a preview and start uploading. - Rejected files show one plain reason each ("12 MB over the 25 MB limit", "640 px wide, listings need at least 1600 px"). - One bad file never blocks the other 79, and nothing the page accepts is promised to publish until the server agrees.

  • Why not hash every photo in the page to find duplicates?
    The common duplicate, the same photo picked twice, is caught for free by name, size and last-modified time. Hashing reads every byte, and the browser's digest call needs the whole input in memory at once: cheap for a 1 MB photo in a worker, out of reach for a 3 GB video on a phone. Spend that cost only if the server asks for it.
  • A photo's File.type says image/jpeg. Does the page know it has a JPEG?
    No. `File.type` is the browser's label, empty when it cannot tell, and it proves nothing about the bytes. Successfully decoding the image is stronger evidence, which the dimension check gets for free. Either way it is a convenience check: the server inspects the stored object again before the listing can publish.

  • Client-side type and size checks stop malicious uploads
  • File.type proves what the file's bytes contain
  • Read all 80 photos into base64 data URLs for the previews
  • An object URL is freed automatically once its image has loaded
  • Decode all 80 photos at once to check their dimensions quickly
  • Reject the whole batch when one file fails a check