// blog

How to Show Medical Images in a Browser Without Uploading Them

A browser can read files the user picks (or drops, or a ZIP) with the File API, without sending them anywhere. A DICOM parser in JavaScript reads the tags and pixels, DecompressionStream inflates deflated data and ZIPs, the browser’s own decoder handles baseline JPEG, and a lookup table applies rescale and window before drawing to a canvas. Check every length before allocating, cap decompression, and say clearly on the page that nothing is uploaded.

Last updated 3 min read

Medical images in the browser, no upload: a browser window showing a chest CT, with the upload cloud crossed out

Most online DICOM viewers start with an upload. For a patient looking at their own scan, that’s a lot to ask. Our DICOM viewer never sees the files: everything happens in the browser tab. Here’s how, and what it takes.

Reading the files

The File API gives a page the files a user picks, drops or selects as a folder (<input type="file" multiple>, webkitdirectory for folders, and drag and drop). file.arrayBuffer() returns the bytes. Nothing leaves the device unless the page sends it.

ZIPs (a portal download, a zipped CD) are just more bytes: read the ZIP’s central directory, inflate each entry, and treat the results like files the user picked.

Parsing DICOM

A DICOM file is a 128-byte preamble, DICM, a meta header, then a list of elements: tag, value representation, length, value. A parser in a few hundred lines of TypeScript handles:

  • implicit and explicit VR, little and big endian;
  • sequences, nested and with undefined lengths;
  • the pixel data, plain or encapsulated in fragments.

Our tag names come from the open-source OFFIS DICOM Toolkit’s data dictionary, exported once to JSON.

Decoding pixels

Encoding In the browser
Uncompressed Read directly
Deflated DecompressionStream('deflate-raw')
RLE lossless A small decoder (PackBits per byte plane)
Baseline JPEG The browser’s own image decoder
JPEG lossless No built-in decoder: a small one in JavaScript, from the JPEG standard’s lossless chapter
JPEG 2000 No built-in decoder: OpenJPEG compiled to JavaScript, loaded only when a file needs it
JPEG-LS No built-in decoder: bring your own (ours is written from the standard)

Drawing

Medical images have more gray levels than a canvas: CT is 12 bits or more. So:

  1. Keep the stored values in a Uint16Array.
  2. Build a lookup table, one entry per possible value, folding in rescale, window and MONOCHROME1 inversion.
  3. Map each pixel through it into ImageData and draw.

Dragging the window rebuilds the table, not the image. Zoom, pan and rotation are canvas transforms.

Limits that keep it safe

Anyone can drop anything on the page, so the parser treats every header as a claim:

  • Check every length and offset against the file before using it.
  • Refuse sizes that would exhaust memory before allocating.
  • Stop decompression at a fixed limit, so a zip bomb can’t fill the tab.
  • Show text from files as text, never as HTML.

We tried to break our own DICOM upload has the full test list, including 1,400 randomly damaged files.

Tell people

Technically private isn’t enough if users don’t know. Say it where they pick the file (“never uploaded”), give them a Close and delete button, and put it in the privacy policy.

Key takeaways

  • The File API reads files locally; nothing is uploaded unless you send it.
  • A small parser plus DecompressionStream covers most files, ZIPs included.
  • A 16-bit lookup table makes window and level instant.
  • Validate every length, cap every decompression, and say what you do.

Open your own files, without uploading →

We’re glad to share what we know. When you want a private viewer in your own portal, we’ll put all of it to work on your project, and because we build with AI-assisted development, it costs far less than you might expect. Let’s talk.

Frequently asked questions

Can the server see the files at all?

Not if the page never sends them. Besides the page's own code, the only thing ours downloads while opening files is the list of DICOM tag names, the same for everyone, and it sends nothing. You can check in the browser's network panel.

What can't a browser decode?

JPEG lossless, JPEG-LS and JPEG 2000 have no built-in browser decoder. Our viewer brings its own: JPEG lossless and JPEG-LS written from their standards, and JPEG 2000 (the open-source OpenJPEG, compiled to JavaScript).

How big a study can it open?

It's limited by the device's memory. We cap the total at 1.5 GB of files and 150 million pixels per image, and decompression at 600 MB, so a hostile file can't exhaust the tab.

Sources

  1. MDN: File API
  2. MDN: DecompressionStream