Forms · Complete runnable example

Local image dropzone

Inspect one local image without uploading it.

Live example

This preview runs in an isolated document. Clipboard writes and saved preferences may be blocked, and printing requires the standalone file. A dialog affects only the example. Reset reloads the original example state.

When to use it

Use this pattern to inspect an image before a separate application step. A native file input provides keyboard access alongside drag and drop. The example accepts one nonempty PNG, JPEG or WebP up to 2 MiB.

Try the important details

  1. Choose a valid image, then inspect its name, dimensions and local preview.
  2. Try an unsupported, empty, oversized or damaged file and read the specific failure message.
  3. Replace or remove the image. Removal returns keyboard focus to the file input.

How it works

  • The input and drop target call the same validation function. The accept attribute helps the picker but is not treated as validation.
  • Each object URL is revoked when replaced, removed, rejected after decoding or left behind on page exit. Stale decode callbacks are ignored.
  • Names and status strings use textContent. No upload endpoint, fetch request or file persistence is present.

The complete source

The preview, copied text, and downloaded file use this same document.

HTML

<!doctype html>
<!--
MIT License
Copyright (c) 2026 MRHTML

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; img-src data: blob:; connect-src 'none'; form-action 'none'; object-src 'none'; base-uri 'none'"
    />
    <title>Keep it local. | MRHTML example</title>
    <style>
      :root {
        color-scheme: light;
        --paper: #f5f3ee;
        --surface: #fff;
        --ink: #172a3b;
        --muted: #536575;
        --line: #d2dce1;
        --blue: #234ed8;
        --soft: #eef3ff;
        --orange: #ad4b17;
        --error: #a62440;
        font:
          16px/1.6 system-ui,
          -apple-system,
          BlinkMacSystemFont,
          'Segoe UI',
          sans-serif;
        color: var(--ink);
        background: var(--paper);
      }
      * {
        box-sizing: border-box;
      }
      body {
        margin: 0;
        padding: clamp(24px, 6vw, 64px) 22px;
        min-height: 100svh;
      }
      main {
        width: min(760px, 100%);
        margin: auto;
        min-width: 0;
      }
      h1,
      h2,
      h3,
      p {
        margin: 0;
      }
      h1 {
        font-size: clamp(2rem, 6vw, 3rem);
        line-height: 1.08;
        letter-spacing: -0.055em;
        font-weight: 680;
        max-width: 680px;
      }
      h2 {
        font-size: 1.2rem;
        line-height: 1.3;
        letter-spacing: -0.02em;
      }
      p {
        color: var(--muted);
      }
      header {
        margin-bottom: 28px;
      }
      .brand {
        font:
          700 0.73rem/1.5 ui-monospace,
          monospace;
        letter-spacing: 0.1em;
        text-transform: uppercase;
        color: var(--blue);
        margin-bottom: 20px;
      }
      .intro {
        max-width: 62ch;
        margin-top: 16px;
      }
      .surface {
        background: var(--surface);
        border: 1px solid var(--line);
        border-radius: 18px;
        padding: clamp(20px, 4vw, 32px);
      }
      .note {
        font-size: 0.85rem;
        margin-top: 20px;
      }
      .eyebrow {
        font:
          600 0.72rem/1.5 ui-monospace,
          monospace;
        letter-spacing: 0.08em;
        text-transform: uppercase;
      }
      .actions {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        margin-top: 20px;
      }
      button,
      input,
      textarea,
      select {
        font: inherit;
      }
      button {
        border: 1px solid var(--ink);
        background: var(--ink);
        color: #fff;
        border-radius: 8px;
        padding: 10px 17px;
        min-height: 44px;
        font-weight: 650;
        cursor: pointer;
      }
      button.secondary {
        background: transparent;
        color: var(--ink);
        border-color: var(--line);
      }
      button:hover:not(:disabled) {
        background: var(--blue);
        border-color: var(--blue);
        color: #fff;
      }
      button:disabled {
        cursor: not-allowed;
        opacity: 0.55;
      }
      label {
        font-weight: 600;
      }
      input:not([type='checkbox']):not([type='range']):not([type='file']),
      textarea,
      select {
        width: 100%;
        min-width: 0;
        border: 1px solid #8e9faa;
        border-radius: 8px;
        padding: 10px 12px;
        background: #fff;
        color: var(--ink);
      }
      textarea {
        resize: vertical;
        min-height: 180px;
      }
      input[type='range'] {
        width: 100%;
        accent-color: var(--blue);
      }
      input[type='checkbox'] {
        width: 20px;
        height: 20px;
        accent-color: var(--blue);
      }
      :focus-visible {
        outline: 3px solid var(--orange);
        outline-offset: 4px;
      }
      a {
        color: var(--blue);
      }
      .status {
        min-height: 1.6em;
        font-size: 0.9rem;
        margin-top: 18px;
        overflow-wrap: anywhere;
      }
      .status[data-error='true'] {
        color: var(--error);
      }
      [hidden] {
        display: none !important;
      }
      .sr-only {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
      }
      .field {
        display: grid;
        gap: 7px;
        margin-top: 16px;
      }
      footer {
        font-size: 0.75rem;
        color: var(--muted);
        border-top: 1px solid var(--line);
        margin-top: 30px;
        padding-top: 16px;
        display: flex;
        justify-content: space-between;
        gap: 12px;
      }
      noscript {
        display: block;
        padding: 14px;
        background: #fff2d9;
        border: 1px solid #ad4b17;
        margin-top: 20px;
        border-radius: 8px;
        color: var(--ink);
      }
      @media (max-width: 400px) {
        body {
          padding: 25px 16px;
        }
        .surface {
          padding: 20px;
        }
        button {
          padding: 10px 14px;
        }
      }
      @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after {
          scroll-behavior: auto !important;
          animation: none !important;
          transition: none !important;
        }
      }

      .dropzone {
        border: 1.5px dashed #8e9faa;
        border-radius: 12px;
        text-align: center;
        padding: 32px 15px;
        background: #f8fafc;
      }
      .dropzone[data-drag='true'] {
        background: var(--soft);
        border-color: var(--blue);
      }
      .dropzone svg {
        color: var(--blue);
        margin-bottom: 16px;
      }
      .dropzone p {
        font-size: 0.85rem;
        margin: 10px 0 20px;
      }
      .file-label {
        display: block;
        margin-bottom: 7px;
      }
      input[type='file'] {
        max-width: 100%;
        font-size: 0.82rem;
      }
      input::file-selector-button {
        padding: 10px;
        border: 1px solid var(--line);
        border-radius: 6px;
        margin-right: 8px;
        background: #fff;
        color: var(--ink);
        font: inherit;
      }
      #preview {
        display: grid;
        grid-template-columns: 140px 1fr;
        gap: 22px;
        border-top: 1px solid var(--line);
        margin-top: 26px;
        padding-top: 26px;
      }
      #preview-image {
        width: 140px;
        height: 130px;
        object-fit: contain;
        background: var(--paper);
        border-radius: 8px;
      }
      .file-info {
        min-width: 0;
      }
      .file-info strong {
        overflow-wrap: anywhere;
      }
      .file-info p {
        font-size: 0.8rem;
        margin: 5px 0 15px;
      }
      @media (max-width: 400px) {
        #preview {
          grid-template-columns: 1fr;
        }
        #preview-image {
          width: 100%;
          height: 150px;
        }
      }
    </style>
  </head>
  <body>
    <main>
      <header>
        <p class="brand">MRHTML / Runnable example</p>
        <h1>Keep it local.</h1>
        <p class="intro">
          Choose an image to inspect before you use it. Files stay in this page;
          there is no upload.
        </p>
      </header>
      <section class="surface">
        <div id="dropzone" class="dropzone">
          <svg
            width="48"
            height="48"
            viewBox="0 0 48 48"
            fill="none"
            aria-hidden="true"
          >
            <rect
              x="7"
              y="9"
              width="34"
              height="30"
              rx="5"
              stroke="currentColor"
              stroke-width="1.5"
            />
            <circle cx="17" cy="19" r="3" fill="currentColor" />
            <path
              d="m9 35 11-10 8 7 5-5 7 8"
              stroke="currentColor"
              stroke-width="1.5"
            />
          </svg>
          <h2>Drop one image here</h2>
          <p id="file-help">PNG, JPEG or WebP. Up to 2 MiB.</p>
          <label class="file-label" for="file-input">Choose an image</label
          ><input
            id="file-input"
            type="file"
            accept="image/png,image/jpeg,image/webp"
            aria-describedby="file-help"
            disabled
          />
        </div>
        <div id="preview" hidden>
          <img id="preview-image" alt="" />
          <div class="file-info">
            <strong id="filename"></strong>
            <p id="filemeta"></p>
            <button id="remove-file" class="secondary" type="button">
              Remove image
            </button>
          </div>
        </div>
        <p id="file-status" class="status" role="status" aria-atomic="true">
          No image selected.
        </p>
      </section>
      <p class="note">
        The checks are for a local preview. A real upload needs independent
        server validation. File names and type declarations are not a security
        boundary.
      </p>
      <noscript
        >JavaScript is needed to validate and preview a local image. No files
        are selected or sent.</noscript
      >
      <footer>
        <span>MRHTML example</span><span>Original code · MIT</span>
      </footer>
    </main>
    <script>
      'use strict';
      (() => {
        const input = document.querySelector('#file-input'),
          zone = document.querySelector('#dropzone'),
          preview = document.querySelector('#preview'),
          image = document.querySelector('#preview-image'),
          status = document.querySelector('#file-status');
        let objectURL = null,
          revision = 0,
          depth = 0;
        const report = (text, error = false) => {
          status.textContent = text;
          status.dataset.error = String(error);
        };
        function release() {
          revision++;
          image.onload = null;
          image.onerror = null;
          image.removeAttribute('src');
          preview.hidden = true;
          if (objectURL) {
            URL.revokeObjectURL(objectURL);
            objectURL = null;
          }
        }
        function choose(files) {
          if (!files.length) return;
          release();
          input.value = '';
          if (files.length !== 1) {
            report('Choose one image at a time.', true);
            return;
          }
          const file = files[0];
          if (!['image/png', 'image/jpeg', 'image/webp'].includes(file.type)) {
            report(
              'That type is not supported. Choose PNG, JPEG or WebP.',
              true,
            );
            return;
          }
          if (file.size === 0 || file.size > 2 * 1024 * 1024) {
            report('Choose a nonempty image no larger than 2 MiB.', true);
            return;
          }
          const current = revision;
          try {
            objectURL = URL.createObjectURL(file);
          } catch {
            report('This browser could not create a local preview.', true);
            return;
          }
          report('Reading image locally…');
          image.onload = () => {
            if (current !== revision) return;
            preview.hidden = false;
            document.querySelector('#filename').textContent = file.name;
            document.querySelector('#filemeta').textContent =
              `${(file.size / 1024).toFixed(1)} KiB · ${image.naturalWidth} × ${image.naturalHeight} px`;
            image.alt = `Local preview: ${file.name}`;
            report('Image ready to inspect. Nothing was uploaded.');
          };
          image.onerror = () => {
            if (current !== revision) return;
            release();
            report('The image could not be decoded. Try another file.', true);
          };
          image.src = objectURL;
        }
        input.disabled = false;
        input.addEventListener('change', () => choose([...input.files]));
        zone.addEventListener('dragenter', (e) => {
          e.preventDefault();
          depth++;
          zone.dataset.drag = 'true';
        });
        zone.addEventListener('dragover', (e) => {
          e.preventDefault();
          if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy';
        });
        zone.addEventListener('dragleave', () => {
          depth = Math.max(0, depth - 1);
          if (!depth) zone.dataset.drag = 'false';
        });
        zone.addEventListener('drop', (e) => {
          e.preventDefault();
          depth = 0;
          zone.dataset.drag = 'false';
          choose([...e.dataTransfer.files]);
        });
        window.addEventListener('dragover', (e) => e.preventDefault());
        window.addEventListener('drop', (e) => e.preventDefault());
        document.querySelector('#remove-file').addEventListener('click', () => {
          release();
          report('Image removed. Nothing was uploaded.');
          input.focus();
        });
        window.addEventListener('pagehide', () => {
          release();
          input.value = '';
          report(
            'Image cleared when leaving this page. Choose it again to preview.',
          );
        });
      })();
    </script>
  </body>
</html>