开始使用
复制一个包含安装步骤和本插件的完整Markdown指南的设置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-file-sharer`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/file-sharer/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
安装bun add @capgo/capacitor-file-sharerbunx cap sync导入
导入import { FileSharer } from '@capgo/capacitor-file-sharer';分享 Base64 文件
分享 Base64 文件import { FileSharer } from '@capgo/capacitor-file-sharer';
await FileSharer.share({ filename: 'report.pdf', contentType: 'application/pdf', base64Data: reportBase64, title: 'Quarterly report', text: 'Attached report',});base64Data 可以是原始 base64 字符串或类似 data:application/pdf;base64,....
分享一个本地文件
Section titled “本地文件共享”await FileSharer.share({ filename: 'export.zip', contentType: 'application/zip', path: fileUri,});原生实现接受本地路径、 file:// URLs、Android content:// URIs、和Capacitor _capacitor_file_ URLs。
const result = await FileSharer.save({ filename: 'backup.zip', contentType: 'application/zip', base64Data: zipBase64, android: { saveDirectory: 'downloads', relativePath: 'Download/My App', },});
console.log(result.uri);Android 保存目录是 downloads, pictures, movies, music, 和 documents在 Android 10 和更高版本中,插件通过 MediaStore 写入。 在 Android 9 和以下版本中,公共保存使用清单 WRITE_EXTERNAL_STORAGE 权限与 maxSdkVersion=28.
分享文本与文件
分享文本与文件await FileSharer.share({ filename: 'photo.jpg', contentType: 'image/jpeg', path: photoUri, title: 'Site photo', subject: 'Photo export', text: 'Captured during inspection.',});text 作为 EXTRA_TEXT 在 Android 上和 iOS 上作为第二个活动项。
类型参考
分享文本与文件ShareFileOptions
复制到剪贴板export interface ShareFileOptions { filename: string; base64Data?: string; path?: string; contentType?: string; text?: string; title?: string; subject?: string; android?: AndroidFileSharerOptions;}AndroidFileSharerOptions
复制到剪贴板export interface AndroidFileSharerOptions { chooserTitle?: string; saveDirectory?: 'downloads' | 'pictures' | 'movies' | 'music' | 'documents'; relativePath?: string;}SaveFileResult
Section titled “SaveFileResult”export interface SaveFileResult { uri?: string;}ERR_PARAM_NO_FILENAME-filename缺少或为空。ERR_PARAM_NO_DATA- 不能提供。base64Data- 不能提供。path- base64输入无法解码。ERR_PARAM_DATA_INVALID- 提供的本地路径或内容URI无法打开。ERR_LOCAL_FILE_NOT_FOUND- 本地临时文件无法写入。ERR_FILE_CACHING_FAILED- Android无法将文件保存到公共存储中。ERR_FILE_SAVE_FAILEDis missing or blank.ERR_ACTIVITY_NOT_FOUND- Android 无法打开分享目标。USER_CANCELLED- iOS 分享面板被关闭而未完成。
真实数据来源
标题:真实数据来源本页面跟踪插件的 src/definitions.ts. 当公共 API 上游发生变化时,请重新同步。