capacitor - 使用电容器共享插件共享文件
问题描述
我需要分享一个pdf文件。我将 CapacitorJS 用于本机功能。
let shareRet = await Share.share({
title: 'See cool stuff',
text: 'Really awesome thing you need to see right meow',
url: 'http://ionicframework.com/',
dialogTitle: 'Share with buddies'
});
这是来自示例。但是我的数据是 base64 字符串。有没有办法把它作为附件分享?
谢谢!
解决方案
此代码适用于我在 iOS 和 Android 上:
import { Directory, Filesystem } from '@capacitor/filesystem';
import { Share } from '@capacitor/share';
function share(fileName: string, base64Data: string) {
return Filesystem.writeFile({
path: fileName,
data: base64Data,
directory: Directory.Cache
})
.then(() => {
return Filesystem.getUri({
directory: Directory.Cache,
path: fileName
});
})
.then((uriResult) => {
return Share.share({
title: fileName,
text: fileName,
url: uriResult.uri,
});
});
}
推荐阅读
- csh - 如何为 .cshrc 中的别名创建单独的文件?
- javascript - Chartist 饼图,下一行需要标签
- java - SHA-256 执行时间小于 java 中的 HmacSHA256
- c++ - 如何在 Xcode 中查看 Auto 的数据类型
- javascript - 无法设置 HMR:在控制台中卡住“等待来自 WDS 的更新信号...”
- pycharm - 如何在 Pycharm 的编辑器中全局增加字体大小?
- amazon-web-services - ECS 实例 AutoScalingGroup - 不稳定 - 扩展活动失败
- python-3.x - 通过布尔表达式在列表理解中返回 none 而不是 value
- c - 如何在 SDL_Renderer 中移动单个元素?
- swift - 当自动重新排序强制集合视图超出最大节数时,在固定大小的集合视图上拖放会崩溃