Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To work with a local file in a web page, let the user choose it with a file picker or drop it onto a drop zone. JavaScript receives a File object that the page can inspect, read or preview; your application can then upload it if that is part of the user’s workflow. A web page cannot use these APIs to browse a visitor’s disk on its own.

Accept files with a picker, a drop zone, or both

A file input is the basic, accessible route. Add multiple if users may select more than one file. Drag-and-drop can make the same workflow more convenient, but should supplement—not replace—the picker. MDN shows how to associate a visible label with a file input so the drop-zone area can also open the picker: MDN: File drag and drop.

<label id="drop-zone" for="file-input">
  Choose files or drop them here
  <input id="file-input" type="file" multiple>
</label>
<ul id="file-list"></ul>

<script>
  const input = document.querySelector("#file-input");
  const zone = document.querySelector("#drop-zone");

  function handleFiles(files) {
    const list = document.querySelector("#file-list");
    list.replaceChildren();

    for (const file of files) {
      const item = document.createElement("li");
      item.textContent = `${file.name} — ${file.size} bytes`;
      list.append(item);
    }
  }

  input.addEventListener("change", () => handleFiles(input.files));

  for (const eventName of ["dragenter", "dragover"]) {
    zone.addEventListener(eventName, event => event.preventDefault());
  }

  zone.addEventListener("drop", event => {
    event.preventDefault();
    handleFiles(event.dataTransfer.files);
  });
</script>

When a page is accepting a drop, call preventDefault() in the relevant drag events, including dragover and drop. Otherwise, the browser may handle the dropped file itself—for example, by opening it or navigating away from the page. The HTML Drag and Drop API documents the event model and file-drop pattern.

The example uses one handler for files from either route. If you instead inspect DataTransfer.items, process only entries that represent files; a drag operation can contain other kinds of data.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Lexar D40E 128GB Dual USB 3.2 Gen 1 Type-C Jump Drive, Champagne Silver
  • USB-C 2-in-1 storage OTG: The Lexar JumpDrive Dual Drive D40E features USB Type-A and Type-C connectors in a slim, portable form factor for easy device compatibility
  • Transfer speeds up to 100MB/s: Based on internal testing, performance may vary depending upon the host device, interface, and usage conditions. 1MB=1,000,000 bytes
  • Plug and Play: Widely compatible with USB Type-C smartphones, tablets, laptops, Macs, and traditional Type-A devices, no software installation required. The 360° swivel design allows for easy switching between connectors without the hassle of losing a cap
  • Durable & Compact: The Lexar D40E USB memory stick features a metal enclosure, withstands temperatures from 0° to 50° C (32°F to 122°F), and is lightweight at 26g with dimensions of 70.4 x 16.9 x 11.7mm
  • Security & Warranty: Securely protects files using an advanced security software solution with 256-bit AES encryption. Backed by a Lexar 3-year limited warranty

Read files in the browser

Each selected file is a File object, a kind of Blob. Its metadata includes name, size, type and lastModified. Use that metadata to display a summary or make preliminary interface decisions, but do not treat the file name or declared media type as proof of what the content contains.

File content can be read asynchronously as text or binary data. The File API includes FileReader and newer reading methods on Blob; choose a method suited to the format and the analysis you need. The MDN File API guide describes the available browser APIs. Parsing large or CPU-intensive files may be better suited to a worker; the File API is available in workers.

Rank #2
SANDISK 128GB Ultra Flair, USB-A Flash Drive, Up to 150MB/s Read Speeds
  • High-speed USB 3.0 performance of up to 150MB/s(1) [(1) Write to drive up to 15x faster than standard USB 2.0 drives (4MB/s); varies by drive capacity. Up to 150MB/s read speed. USB 3.0 port required. Based on internal testing; performance may be lower depending on host device, usage conditions, and other factors; 1MB=1,000,000 bytes]
  • Transfer a full-length movie in less than 30 seconds(2) [(2) Based on 1.2GB MPEG-4 video transfer with USB 3.0 host device. Results may vary based on host device, file attributes and other factors]
  • Transfer to drive up to 15 times faster than standard USB 2.0 drives(1)
  • Sleek, durable metal casing
  • Easy-to-use password protection for your private files(3) [(3)Password protection uses 128-bit AES encryption and is supported by Windows 7, Windows 8, Windows 10, and Mac OS X v10.9 plus; Software download required for Mac, visit the SanDisk SecureAccess support page]

For example, to read a text file with a promise-based Blob method:

async function readText(file) {
  return await file.text();
}

Reading locally is useful when the page needs to show a preview or analyze content before submission. It is not required just to upload a file: an upload request can send the File or Blob directly through an upload-capable web API. The File API documentation describes its integration with other web platform APIs, including XMLHttpRequest.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
2 Pack 64GB USB Flash Drive USB 2.0 Thumb Drives Jump Drive Fold Storage Memory Stick Swivel Design - Black
  • What You Get - 2 pack 64GB genuine USB 2.0 flash drives, 12-month warranty and lifetime friendly customer service
  • Great for All Ages and Purposes – the thumb drives are suitable for storing digital data for school, business or daily usage. Apply to data storage of music, photos, movies and other files
  • Easy to Use - Plug and play USB memory stick, no need to install any software. Support Windows 7 / 8 / 10 / Vista / XP / Unix / 2000 / ME / NT Linux and Mac OS, compatible with USB 2.0 and 1.1 ports
  • Convenient Design - 360°metal swivel cap with matt surface and ring designed zip drive can protect USB connector, avoid to leave your fingerprint and easily attach to your key chain to avoid from losing and for easy carrying
  • Brand Yourself - Brand the flash drive with your company's name and provide company's overview, policies, etc. to the newly joined employees or your customers

Preview files with an object URL

For a local preview, create an object URL from a selected file and use it where the relevant element accepts a URL, such as an image source. Revoke the URL when the preview is replaced or cleared so the page can release the associated resource.

let previewUrl;

function showImage(file, image) {
  if (previewUrl) URL.revokeObjectURL(previewUrl);
  previewUrl = URL.createObjectURL(file);
  image.src = previewUrl;
}

function clearImage(image) {
  image.removeAttribute("src");
  if (previewUrl) {
    URL.revokeObjectURL(previewUrl);
    previewUrl = undefined;
  }
}

Object URLs provide a way to reference a file or blob; they do not establish that its content is safe. See MDN’s URL.createObjectURL() reference and URL.revokeObjectURL() reference.

Rank #4
SIMMAX 32GB Memory Stick USB 2.0 Flash Drives Swivel Thumb Drive Pen Drive (32GB Purple)
  • GOOD VALUE PACKAGE - 1 Pack 32GB Memory Stick USB 2.0 Flash Drives with great cost performance and high quality.
  • BIG CAPACITY - The available capacity: 29.10GB-29.8GB, You can save the data of movies, music, photos, designs, programs, manuals, handouts in a high speed.Good performance in digital data storing, transferring and sharing with families, friends, workmates, clients and machines.
  • EASY TO USE & PLUG AND WORK - Support windows 7 / 8 / 10 / Vista / XP / 2000 / ME / NT Linux and Mac OS, Compatible with USB2.0 and below.
  • TWISTTURN DESIGN & EASY CARRY - The metal clip rotates 360° round the ABS plastic body which with rubber oil skin feeling finish. The capless design can avoid lossing of cap, and providing efficient protection to the USB port.
  • WARRANTY & SUPPORT - SIMMAX logo is laser printed on the USB connector surface, our products are of good quality and we promise that any problem about the product within one year since you buy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose whether analysis happens locally or on a server

The right location depends on what the feature must do and what the user expects to happen to the file. Local processing supports immediate previews or analysis without sending the file as part of that step. Server processing is needed when the application’s workflow requires a server to store, transform or otherwise handle the upload. Make the transfer intentional and explain clearly what will be read or sent.

Choice What happens Best fit
Browser-side reading The page reads the selected File or Blob in the browser. Local previews, user-facing analysis, or processing that does not require a server.
Direct upload The page sends the selected file through an upload-capable web API. Workflows that require server-side storage or processing; reading the whole file in JavaScript first is not inherently necessary.
Read, then upload The page analyzes or previews content locally, then sends it when the user proceeds. Flows where users should inspect or analyze a file before choosing to submit it.

The File API’s access model is user-mediated: files are made available through a file input or drop operation. The W3C File API specification discusses safeguards around user-selected files and access to sensitive system files. Tell users what the page will read or upload and provide a clear action for initiating any transfer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Sale
IMEASON Swivel Design 16GB USB Flash Drive with Keychain, USB 2.0 Portable Thumb Drive Memory Stick, FAT32 Format Flashdrive for Data Storage, Photos, Music, Files (Black, 16 GB)
  • 【16GB Flash Drive】USB flash drives with 16GB capacity, meet your needs of daily use on work, school, home and travelling for photos, music, videos, files storage and transfer. IMEASON thumb drives can be used to store different files, easy to data backup.
  • 【Metal Swivel Cap Design】USB thumb drive is metal swivel cover provides extra protection for the usb thumbdrive connector, no usb drive cap to lose; keychain design makes it easier to carry without worrying lose it.
  • 【Wide Compatibility】USB drive supports Windows 7/8/10/11 / Vista / XP / Unix / 2000 / ME / NT Linux and Mac OS, also Supports USB 2.0 and 1.1 ports. USB Stick support TV, desktop, notebook computer, car, audio and other device. The USB Memory Stick is your great data storage and transfer companion with traveling and working.
  • 【Easy to use】usb memory stick is plug and play without any software installation. Just simply plug the Flashdrive into the port of your USB-compatible devices such as computer, laptop to start data storage or transmission.
  • 【What You Get】16 GB USB Flash Drive Thumb Drive, The default format of the usb storage flash drive is FAT32.

Validate uploads on the server

Browser-side checks can quickly flag unsupported types or files that exceed a user-facing limit, but they are convenience checks, not a security boundary. A sender can control request data and metadata, so the server must apply its own validation and storage rules.

  • Allow only file types and extensions the application needs.
  • Check the actual content rather than trusting the request’s declared Content-Type.
  • Set file-size limits. If archives are accepted, also limit the resources used when extracting them.
  • Generate safe names for stored files rather than using an uploaded name as a storage path.
  • Control where files are stored and who can access them.
  • Do not rely on an extension or a signature check alone as complete protection.

These are defense-in-depth recommendations from the OWASP File Upload Cheat Sheet, which also discusses malicious content and resource-exhaustion risks.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.