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

问题描述

我对Javascript和Ionic框架很陌生。我想使用 SocialShare Plugin共享一个路径为“ /assets/input.json”的本地文件,并且希望通过该应用程序将该扩展名为.json的本地文件共享为.txt文件。>

任何人都可以帮助我如何使用此插件访问本地文件,以及如何将其转换为文本文件以进行共享。

解决方法

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

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',you can provide id of the App you want to share with
  iPadCoordinates: '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读写文件

,

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

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);
  })
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...