@capacitor/file-viewer
FileViewer API 提供了打开文件和预览媒体的功能。该功能在 Web 平台上不可用。
媒体预览功能目前仅支持 iOS 平台,使用系统内置的播放器实现。
安装
npm install @capacitor/file-viewer
npx cap sync
示例
import { FileViewer } from '@capacitor/file-viewer';
// 可以使用如 @capacitor/filesystem 这样的插件获取文件的完整路径
const openDocument = async () => {
await FileViewer.openDocumentFromLocalPath({
path: 'path/to/file.pdf',
});
};
// iOS 特有功能
const previewMedia = async () => {
await FileViewer.previewMediaContentFromUrl({
path: 'https://url_hosting_media/file.mp4',
});
};
API
现有错误代码列表,请参见错误处理。
文件查看器 API
仅适用于原生 Android 和 iOS 平台;不适用于 Web/PWA。
openDocumentFromLocalPath(...)
openDocumentFromLocalPath(options: OpenFromLocalPathOptions) => Promise<void>
打开本地文件系统中存储的文件
| 参数 | 类型 |
|---|---|
options | |
Since: 1.0.0
openDocumentFromResources(...)
openDocumentFromResources(options: OpenFromResourcesOptions) => Promise<void>
打开应用资源文件
| 参数 | 类型 |
|---|---|
options | |
Since: 1.0.0
openDocumentFromUrl(...)
openDocumentFromUrl(options: OpenFromUrlOptions) => Promise<void>
从远程 URL 打开文件
| 参数 | 类型 |
|---|---|
options | |
Since: 1.0.0
previewMediaContentFromLocalPath(...)
previewMediaContentFromLocalPath(options: PreviewMediaFromLocalPathOptions) => Promise<void>
预览本地文件系统中存储的媒体文件(主要是视频)。
仅在 iOS 平台实现。Android 平台默认使用 openDocumentFromLocalPath。
| 参数 | 类型 |
|---|---|
options | |
Since: 1.0.0
previewMediaContentFromResources(...)
previewMediaContentFromResources(options: PreviewMediaFromResourcesOptions) => Promise<void>
预览应用资源中的媒体文件(主要是视频)。
仅在 iOS 平台实现。Android 平台默认使用 openDocumentFromResources。
| 参数 | 类型 |
|---|---|
options | |
Since: 1.0.0
previewMediaContentFromUrl(...)
previewMediaContentFromUrl(options: PreviewMediaFromUrlOptions) => Promise<void>
从远程 URL 预览媒体文件(主要是视频)。
仅在 iOS 平台实现。Android 平台默认使用 openDocumentFromUrl。
| 参数 | 类型 |
|---|---|
options | |
Since: 1.0.0