首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Ionic5项目中使用SocialShare插件共享本地文件?

如何在Ionic5项目中使用SocialShare插件共享本地文件?
EN

Stack Overflow用户
提问于 2020-09-13 00:17:30
回答 2查看 601关注 0票数 0

我是Javascript和Ionic框架的新手。我想使用 SocialShare Plugin共享一个路径为"/assets/input.json“的本地文件,我想通过应用程序将这个扩展名为.json的本地文件共享到一个.txt文件中。

谁能帮助我应该如何使用这个插件来访问我的本地文件,以及如何将其转换为文本文件以共享它。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-15 15:15:11

尝试这个参考:https://www.npmjs.com/package/cordova-plugin-x-socialsharing

代码语言:javascript
复制
var options = {
  message: 'share this', // not supported on some apps (Facebook, Instagram)
  subject: 'the subject', // fi. for email
  files: ['', ''], // an array of filenames either locally or remotely
  url: 'https://www.website.com/foo/#bar?a=b',
  chooserTitle: 'Pick an app', // Android only, you can override the default share sheet title
  appPackageName: 'com.apple.social.facebook', // Android only, you can provide id of the App you want to share with
  iPadCoordinates: '0,0,0,0' //IOS only iPadCoordinates for where the popover should be point.  Format with x,y,width,height
};

对于从.json到.txt的文件转换,您可以使用js (https://www.websparrow.org/web/how-to-create-and-save-text-file-in-javascript)和离子库https://ionicframework.com/docs/native/file读写文件

票数 0
EN

Stack Overflow用户

发布于 2020-10-08 22:06:22

而不是社交共享,我建议使用Filesharer,在社交共享中,我真的很怀疑你能从你的文件夹中共享一个本地文件。我也有类似的要求,Filesharer是一个完美的插件,下面是你可以尝试的代码。

代码语言:javascript
复制
async shareLocalFile() {
   console.log('Sharing files...')
   this.http.get('/assets/input.json', { responseType: 'blob'})
   .subscribe( res => {
     const reader = new FileReader();
     reader.onloadend = () => {
       const result = reader.result as string;
       const base64Data = result.split(',')[1]
       FileSharer.share({
         filename:  'input.txt',
         base64Data,
         contentType:'application/txt'
       });
     }reader.readAsDataURL(res);
  })
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63862388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档