[英]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.