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

To upload an image from a browser, let the user choose it with an <input type="file">, then send the selected File to an application endpoint. For an in-page upload, use FormData with fetch(); for a standard form submission, use method="post" and enctype="multipart/form-data". In either case, the server—not the browser—must receive, validate, and process the file.

Choose between a native form and JavaScript with fetch()

Approach What happens Best fit
Native HTML form The browser submits the form and usually navigates to the server response. A straightforward upload where a full-page response is acceptable and minimal client-side code is preferred.
JavaScript-managed form JavaScript intercepts submission and sends a FormData object using fetch(), so the page can remain in place. An interface that needs to update status or provide other in-page behavior.

Both methods require an application endpoint that understands the submitted multipart form data. Using JavaScript does not store the image or remove the need for server-side upload handling. MDN explains the native pattern in its guide to sending form data and the JavaScript pattern in its guide to sending forms through JavaScript.

Build a basic image upload form

Give the file input a name that matches the field expected by your server. The browser exposes the selected file through the input’s files property; its value is not the file’s bytes.

<form id="image-form">
  <label for="image">Choose an image</label>
  <input id="image" name="image" type="file" accept="image/*" required>
  <button type="submit">Upload</button>
  <p id="status" role="status"></p>
</form>

The accept="image/*" attribute guides the file picker; it does not prove that a submitted file is an image. You can use narrower hints such as accept=".jpg,.jpeg,.png" if that suits the interface, but the server must still validate the actual upload. See MDN’s reference for the accept attribute and file inputs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Upload the selected image with fetch()

This example intercepts form submission, constructs FormData from the form, sends it to /uploads, and checks the HTTP response before showing success. Replace /uploads with the route implemented by your application.

const form = document.querySelector("#image-form");
const status = document.querySelector("#status");

form.addEventListener("submit", async (event) => {
  event.preventDefault();

  const fileInput = form.elements.image;
  const file = fileInput.files[0];
  if (!file) {
    status.textContent = "Choose an image first.";
    return;
  }

  const formData = new FormData(form);

  try {
    const response = await fetch("/uploads", {
      method: "POST",
      body: formData,
    });

    if (!response.ok) {
      throw new Error(`Upload failed: ${response.status}`);
    }

    status.textContent = "Upload complete.";
  } catch (error) {
    status.textContent = "Upload failed. Please try again.";
    console.error(error);
  }
});

When you pass FormData as the request body, do not set the Content-Type header yourself. The browser needs to add the multipart boundary that separates the fields and file data. MDN documents this behavior in Using FormData Objects.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

A successful call to fetch() does not by itself mean the upload succeeded: Fetch provides a Response even for HTTP error statuses, so check response.ok. Read a response as JSON only if the endpoint actually returns JSON. See MDN’s Fetch API guide.

Use a native multipart form when JavaScript is unnecessary

If a full-page submission is acceptable, the browser can submit the file without a JavaScript handler:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
<form action="/uploads" method="post" enctype="multipart/form-data">
  <label for="image-native">Choose an image</label>
  <input id="image-native" name="image" type="file" accept="image/*" required>
  <button type="submit">Upload</button>
</form>

Use POST, multipart/form-data, and a named file input. Multipart encoding can carry the file alongside ordinary form fields. The server’s route and parser must agree with the form’s action and field name.

Send one file directly or handle multiple selections

Append a selected File explicitly

If your interface does not use a form, create FormData and append the selected file under the field name your endpoint expects:

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
const input = document.querySelector("#image");
const file = input.files[0];

if (file) {
  const data = new FormData();
  data.append("image", file, file.name);

  const response = await fetch("/uploads", {
    method: "POST",
    body: data,
  });

  if (!response.ok) {
    throw new Error(`Upload failed: ${response.status}`);
  }
}

Allow multiple images only when the endpoint supports them

Add the multiple attribute only if your product accepts multiple selections. Then iterate through input.files and append each file using the field convention expected by the server. Whether the endpoint expects repeated fields named image, fields named images[], or another structure is application-specific; align the client with its parser rather than assuming a convention.

const input = document.querySelector("#images");
const data = new FormData();

for (const file of input.files) {
  data.append("images", file, file.name);
}

const response = await fetch("/uploads", {
  method: "POST",
  body: data,
});

if (!response.ok) {
  throw new Error(`Upload failed: ${response.status}`);
}
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Validate uploads at the application boundary

Client-side checks can make the interface clearer, but they are not a security boundary. A file’s name, size, and MIME type are useful for displaying hints or catching obvious mistakes, yet client-provided metadata must not be your server’s sole validation. The server should enforce the file types and size limits it supports, authenticate or authorize the request where required, and apply its own safe storage and serving policies. The allowed types, size limits, storage destination, and response format depend on your application and deployment; browser APIs do not define universal values.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Handle cross-origin upload endpoints correctly

If the page and upload endpoint have different origins, the endpoint must have suitable CORS configuration for browser JavaScript to read its response. Some requests also cause a preflight request. Do not use mode: "no-cors" as a workaround: it produces an opaque response whose body and headers JavaScript cannot inspect. Configure the server for the origins and request behavior your application needs, then handle the actual response status in the client.

Troubleshoot common upload failures

  • The server receives no file: Confirm the input has a name, that the name matches the server parser, and that the file is included in the submitted FormData.
  • The server cannot parse multipart data: When using FormData, remove any manually set Content-Type: multipart/form-data header so the browser can supply the boundary. For a native form, confirm enctype="multipart/form-data" is present.
  • The interface reports success for a failed upload: Check response.ok or response.status; Fetch does not reject merely because the server returned an HTTP error status.
  • The picker allows a file the server rejects: This can happen because accept is a picker hint, not validation. Make the server’s accepted formats clear in the interface and enforce them on the server.
  • The request works but JavaScript cannot read the response: Check the endpoint’s CORS response headers and any preflight handling. no-cors will not make an unreadable response usable.
  • A large upload fails: Check request and upload size limits in the application and deployment. There is no universal browser upload maximum established by these APIs.

Or skip the browser setup

ScreenshotNeo is a website screenshot API, not a service for uploading a user-selected image to your application. If what you need is a screenshot of a webpage rather than an image-file upload, one request can return a screenshot. The API accepts a URL and can return PNG, JPEG, WebP, or PDF; see the ScreenshotNeo site and API documentation.

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The Free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for ScreenshotNeo’s free plan.

Frequently Asked Questions

Does the browser save an uploaded image for my application?

No. The browser sends the selected file to your endpoint; your application must decide how to process and store it.

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

Can I use the same JavaScript example with any upload URL?

Only if that route accepts a multipart POST with the field name used by the form. The endpoint’s parser and response contract determine what the client should send and read.

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.