Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: share file #2668

Closed
3 of 4 tasks
cmaas opened this issue Mar 31, 2020 · 7 comments
Closed
3 of 4 tasks

feat: share file #2668

cmaas opened this issue Mar 31, 2020 · 7 comments

Comments

@cmaas
Copy link

cmaas commented Mar 31, 2020

Feature Request

Describe the Feature Request

The share sheet in iOS and Android allows to share or download files with other apps. The current share plugin in Capacitor seems to only allow sharing text.

I'd like to share a file that is either generated on the fly (think: export a data set as CSV or JSON) or from the app's filesystem.

Platform Support Requested

  • Android
  • iOS
  • Electron
  • Web

Describe Preferred Solution

await Share.share({
    title: 'Download stats',
    text: JSON.stringify(myStats),
    url: 'stats.json',
    dialogTitle: 'Download Stats'
});

Describe Alternatives

Alternative might be to create a file first in the local filesystem via the Filesystem plugin and then reference this file with the Share plugin.

@jcesarmobile
Copy link
Member

Sharing files has always worked on iOS if using the url, but has to be a proper url, not just a file name.
On Android was recently fixed #2338

@cmaas
Copy link
Author

cmaas commented Mar 31, 2020

@jcesarmobile Thanks! Could you tell me what the proper url looks like? Do I have to retrieve it via the Filesystem plugin? The docs are incomplete for this aspect.

@eweap
Copy link

eweap commented Aug 31, 2020

I would also like to know how to share a file! @jcesarmobile maybe an example is available somewhere ?

Thanks.

@cmaas
Copy link
Author

cmaas commented Aug 31, 2020

@eweap Figured it out:

// generate a ZIP via JSZip
const archive = await zip.generateAsync({ type: 'base64', compression: 'DEFLATE' });

try {
    const result = await Filesystem.writeFile({
        path: `Your-Filename.zip`,
        data: archive,
        directory: FilesystemDirectory.Documents
    });
    console.log('Wrote file', result);
    let shareRet = await Share.share({
        title: 'My File',
        url: result.uri,
        dialogTitle: 'Save file'
    });
} catch (e) {
    console.error('Unable to write file', e);
}

@eweap
Copy link

eweap commented Aug 31, 2020

@cmaas thanks ! I will try it tomorrow !

@cmaas
Copy link
Author

cmaas commented Sep 1, 2020

@eweap One thing though: On Android 10, writing a file is a bit different and requires a special permission with the current FileSystem plugin. Not sure if this is already marked as an issue in Capacitor. However, this is the fix:

<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
    Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
   ...
 </application>
</manifest>

https://stackoverflow.com/questions/60569224/cant-write-to-android-external-storage-even-with-permissions-set

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 11, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants