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
Looking for something else?
Manifest configuration (which file types to handle): See File Handlers documentation
Client-side implementation (how to process files): You're on the right page!
Complete quick start example: See Quick Start in File Handlers
How It Works
The File Handling feature consists of two parts:
Manifest Configuration: Declare which file types your PWA can handle (see File Handlers documentation)
Client-Side Handling: Receive and process files using the Stimulus controller (this page)
Complete Workflow
Key Points:
The manifest
actionURL must match the page where you add the Stimulus controllerThe
acceptconfiguration in the manifest determines which files trigger your PWAEach file generates a separate
pwa--file-handling:selectedeventThe 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.
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
pwa--file-handling:selectedDispatched 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
Validate file types: Always validate that received files match expected types
Handle errors gracefully: File loading can fail for various reasons
Show loading states: Provide feedback while processing files
Support multiple files: The event fires once per file, handle multiple files properly
Cleanup resources: Revoke blob URLs when done to free memory
Declare file handlers: Properly configure manifest file handlers
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
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?