@napacitor/file-viewer
File viewer plugin (corresponds to @capacitor/file-viewer).
Installation
To use npm:
npm install @napacitor/file-viewer
To use yarn:
yarn add @napacitor/file-viewer
Sync native files:
npx nap sync
Usage
import { FileViewer } from '@napacitor/file-viewer';
// Local absolute path
await FileViewer.openDocumentFromLocalPath({ path: '/storage/emulated/0/Download/sample.pdf' });
// Resource inside the app package (relative path under Resources/Raw)
await FileViewer.openDocumentFromResources({ path: 'docs/sample.pdf' });
// Remote file (handled by the system browser/downloader)
await FileViewer.openDocumentFromUrl({ url: 'https://example.com/sample.pdf' });
How It Works
Wraps the APIs described in the .NET MAUI Launcher.Default.OpenAsync and FileSystem documentation: an OpenFileRequest + ReadOnlyFile is handed to the system viewer (Android: Intent.ACTION_VIEW / Windows: the system default open action). Resource files are read via FileSystem.Current.OpenAppPackageFileAsync, copied to the cache directory, and then handed to the viewer (packaged resources cannot be given to an external viewer directly as file paths).
Permissions
Android
No additional configuration required. Reading the file pointed to by path requires the app to have the corresponding storage access permission (such as READ_EXTERNAL_STORAGE, depending on the target file location).
iOS
No additional configuration required.
Windows
No additional configuration required.
API
openDocumentFromLocalPath(options)
Opens a file from the local file system (requires a viewer capable of handling the file type; throws INVALID_ARGUMENTS when the file does not exist).
| Param | Type |
|---|---|
options | OpenFromLocalPathOptions |
Returns: Promise<void>
openDocumentFromResources(options)
Opens a resource file inside the app package (copied to the cache directory and then handed to the system viewer; throws INVALID_ARGUMENTS when the resource does not exist).
| Param | Type |
|---|---|
options | OpenFromResourcesOptions |
Returns: Promise<void>
openDocumentFromUrl(options)
Opens a remote file (handled by the system; throws INVALID_ARGUMENTS when the URL is invalid).
| Param | Type |
|---|---|
options | OpenFromUrlOptions |
Returns: Promise<void>
Interfaces
OpenFromLocalPathOptions
| Prop | Type | Description |
|---|---|---|
path | string | Absolute path of the file (must not be empty, otherwise INVALID_ARGUMENTS is thrown). |
OpenFromResourcesOptions
| Prop | Type | Description |
|---|---|---|
path | string | Relative path of a resource inside the app package (under MAUI Resources/Raw, e.g. 'docs/sample.pdf'). |
OpenFromUrlOptions
| Prop | Type | Description |
|---|---|---|
url | string | Remote URL pointing to a file. Handled by the system browser/downloader; the file is not downloaded and opened in a document viewer automatically (for that behavior, download it over HTTP to a local file first and call openDocumentFromLocalPath). |