简体   繁体   English

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

[英]How to share a local file using SocialShare Plugin in Ionic5 project?

I am pretty new to Javascript and Ionic framework .我对 Javascript 和 Ionic 框架很陌生。 I want to share a local file which has the path "/assets/input.json" using SocialShare Plugin and I want to share this local file which is of .json extension into a .txt file through the app.我想使用SocialShare 插件共享一个路径为“/assets/input.json”的本地文件,我想通过应用程序将这个扩展名为.json 的本地文件共享到一个 .txt 文件中。

Can anyone please help with how should I use this plugin to access my local file and how can I convert it into a text file for sharing it.任何人都可以帮助我如何使用此插件访问我的本地文件以及如何将其转换为文本文件以进行共享。

Rather than Social Sharing , I would suggest to use Filesharer , In social sharing I really doubt that you can share a local file from one of your folder.我建议使用Filesharer而不是 Social Sharing ,在社交共享中,我真的怀疑您是否可以从您的文件夹中共享本地文件。 I also have the similar requirement and Filesharer is a perfect plugin for that, below is the code you can try.我也有类似的需求,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); }) }

Try this Reference : https://www.npmjs.com/package/cordova-plugin-x-socialsharing试试这个参考: 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', // 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
};

For file conversion from .json to .txt you can read & write file using js ( https://www.websparrow.org/web/how-to-create-and-save-text-file-in-javascript ) and ionic libraries https://ionicframework.com/docs/native/file对于从 .json 到 .txt 的文件转换,您可以使用 js ( https://www.websparrow.org/web/how-to-create-and-save-text-file-in-javascript ) 和 ionic 读写文件库https://ionicframework.com/docs/native/file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用Ionic中的webintent插件显示android共享扩展名的额外文本 - How can i display extra text for android share extension using webintent plugin in ionic 如何在离子中显示共享底页以进行文件共享? - how to show share bottomsheet in ionic for file sharing? Ionic 6 Capacitor 3 Share 插件不适用于 Android 12 - Ionic 6 Capacitor 3 Share Plugin Not Working with Android 12 如何从使用 Ionic Framework 中的 Camera Plugin 选择的视频文件 URI 创建 Blob? - How to create a Blob from video File URI selected using Camera Plugin in Ionic Framework? 硬件后退按钮被调用两次 ionic5 - hardware back button is called twice ionic5 如何使用ionic 5中的离子相机插件将图像存储在数据库中 - how to store image in the data base using ionic camera plugin in ionic 5 如何在eclipse中创建新的android项目并在本地git存储库中共享? - How to create new android project in eclipse and share in local git repository? 如何在ionic项目(android)中安装phonegap“推送插件”? - how to install phonegap “push plugin” in ionic project(android)? 如何将android项目转换为离子插件 - How can I convert android project to ionic plugin 如何使用cordova-plugin-file在cordova应用程序之间共享数据? (Android,IOS) - How to share data between cordova apps using cordova-plugin-file? (Android, IOS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM