File Handling

The File Handling component provides an interface to the File Handling API (Launch Queue API), enabling your Progressive Web App to receive and process files that users open through the operating system. This makes your PWA behave like a native application that can be associated with specific file types.

This component is particularly useful for:

  • Image editors and viewers

  • Document processors (PDF, Office files)

  • Media players (audio, video)

  • Code editors and IDEs

  • Design tools (SVG, CAD files)

  • Data import/export applications

circle-check

How It Works

The File Handling feature consists of two parts:

  1. Manifest Configuration: Declare which file types your PWA can handle (see File Handlers documentation)

  2. Client-Side Handling: Receive and process files using the Stimulus controller (this page)

Complete Workflow

Key Points:

  • The manifest action URL must match the page where you add the Stimulus controller

  • The accept configuration in the manifest determines which files trigger your PWA

  • Each file generates a separate pwa--file-handling:selected event

  • The event provides a blob URL that you can use immediately

This page focuses on steps 4-5 (client-side implementation). For step 3 (manifest configuration), see the File Handlers manifest documentation.

Browser Support

The File Handling API is currently supported in Chromium-based browsers (Chrome, Edge) on desktop platforms. Support on mobile and other browsers is limited or not available.

Prerequisites

Step 1: Configure Manifest File Handlers

Before your PWA can receive files, you must declare the file types it can handle in the manifest. This tells the operating system to associate your PWA with specific file extensions.

circle-info

The action parameter should point to the route/page where you'll add the Stimulus controller (Step 2). See the File Handlers manifest documentation for detailed configuration options including wildcards, multiple handlers, and advanced URL configuration.

Step 2: Add the Stimulus Controller

Add the @pwa/file-handling controller to the template/page specified in the manifest's action parameter:

The controller automatically listens for files passed through the Launch Queue API and dispatches the pwa--file-handling:selected event for each file.

Usage

Basic Image Viewer

Photo Editor with Multiple Files

Document Viewer (PDF, Text)

Media Player with Playlist

Code Editor with Syntax Highlighting

CSV Data Importer

Parameters

None

Actions

None - This controller automatically listens for files passed through the Launch Queue API.

Targets

None

Events

pwa--file-handling:selected

Dispatched for each file that the app receives through the Launch Queue API. This event is triggered when:

  • User right-clicks a file and selects "Open with..." your PWA

  • User double-clicks a file associated with your PWA

  • File is dragged onto your PWA icon

Payload: {data}

  • data (string): A blob URL or data URL representing the file content

Example:

Best Practices

  1. Validate file types: Always validate that received files match expected types

  2. Handle errors gracefully: File loading can fail for various reasons

  3. Show loading states: Provide feedback while processing files

  4. Support multiple files: The event fires once per file, handle multiple files properly

  5. Cleanup resources: Revoke blob URLs when done to free memory

  6. Declare file handlers: Properly configure manifest file handlers

  7. Test thoroughly: Test with various file types and sizes

File Type Detection

Resource Cleanup

Always clean up blob URLs to prevent memory leaks:

Complete Example: Multi-Format File Viewer

Browser Compatibility

Feature
Chrome
Edge
Safari
Firefox

File Handling API

✅ 102+

✅ 102+

Launch Queue

✅ 102+

✅ 102+

Always provide alternative ways to open files (file input, drag-and-drop) for browsers that don't support the File Handling API.

Last updated

Was this helpful?