开始入门
复制一个包含安装步骤和本插件完整 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.
You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:
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);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 在 Android 上作为和 iOS 上作为第二个活动项传递。 EXTRA_TEXT 类型参考
Section titled “Type Reference”
Section titled “ShareFileOptions”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既base64Data也path未提供。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_CANCELLEDiOS分享面板未完成时被关闭。
数据源
数据源本页面跟踪插件的 src/definitions.ts当公共API上游发生变化时,请重新运行同步。
继续从开始
继续从开始如果您正在使用 开始 规划存储和文件处理,请将其与 使用@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 的本机能力