开始获取
复制一个包含安装步骤和本插件的完整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.
您可以使用我们的AI辅助设置来安装插件。将Capgo技能添加到您的AI工具中,使用以下命令:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下提示:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-file-sharer` plugin in my project.如果您更喜欢手动设置,请运行以下命令安装插件,并按照以下平台特定的说明进行操作:
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字符串或数据URL,如 data:application/pdf;base64,....
分享本地文件
标题:分享本地文件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.
Share Text With A File
标题:Share Text With A Fileawait 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
标题:ShareFileOptionsexport interface ShareFileOptions { filename: string; base64Data?: string; path?: string; contentType?: string; text?: string; title?: string; subject?: string; android?: AndroidFileSharerOptions;}AndroidFileSharerOptions
AndroidFileSharerOptionsexport interface AndroidFileSharerOptions { chooserTitle?: string; saveDirectory?: 'downloads' | 'pictures' | 'movies' | 'music' | 'documents'; relativePath?: string;}SaveFileResult
SaveFileResultexport interface SaveFileResult { uri?: string;}错误代码
错误代码ERR_PARAM_NO_FILENAME-filename缺少或为空。ERR_PARAM_NO_DATA既没有base64Data也没有path未提供。ERR_PARAM_DATA_INVALIDbase64输入无法解码。ERR_LOCAL_FILE_NOT_FOUND- 提供的本地路径或内容 URI 无法打开。ERR_FILE_CACHING_FAILED- 原生临时文件无法写入。ERR_FILE_SAVE_FAILED- Android 无法将文件保存到公共存储。ERR_ACTIVITY_NOT_FOUND- Android 无法打开分享目标。USER_CANCELLED- iOS 分享面板被dismiss而未完成。
真实来源
标题:真实来源本页面跟踪插件的 src/definitions.ts当公共 API 上游发生变化时,请重新运行同步。
从 Getting Started 继续
标题:从 Getting Started 继续如果您正在使用 开始使用 为存储和文件处理做好准备,连接它与 使用 @capgo/capacitor-file-sharer 为 @capgo/capacitor-file-sharer 原生能力 @capgo/capacitor-data-storage-sqlite 为 @capgo/capacitor-data-storage-sqlite 实现细节 使用 @capgo/capacitor-data-storage-sqlite 为 @capgo/capacitor-data-storage-sqlite 原生能力 @capgo/capacitor-file 为 @capgo/capacitor-file 实现细节, 使用 @capgo/capacitor-file 为 @capgo/capacitor-file 原生能力.