首页 > 解决方案 > 使用电容器共享插件共享文件

问题描述

我需要分享一个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 字符串。有没有办法把它作为附件分享?

谢谢!

标签: capacitor

解决方案


此代码适用于我在 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,
      });
    });
}

推荐阅读