开始使用
复制一个包含安装步骤和此插件的完整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.如果您prefer手动设置,安装插件并运行以下命令,然后按照以下平台特定的说明:
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,....
分享本地文件
分享本地文件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);安卓保存目录是 downloads, pictures, movies, music, 和 documents。 在安卓 10 及以上版本中,插件通过 MediaStore 写入。 在安卓 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 的参数传递,
类型参考
类型参考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
保存文件结果export interface SaveFileResult { uri?: string;}错误代码
错误代码ERR_PARAM_NO_FILENAME-filename不能为空ERR_PARAM_NO_DATA都不能base64Datanorpath没有提供。ERR_PARAM_DATA_INVALID- base64输入无法解码。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时,重新运行同步,当公共API在上游发生变化时。
继续从 Getting Started
标题:继续从 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 文件的实现细节 使用 @capgo/capacitor 文件 为在使用 @capgo/capacitor 文件中native能力