import { Directory, Encoding, Filesystem } from '@capacitor/filesystem'; import { Share } from '@capacitor/share'; // The iOS and Android web views ignore `` links, so the browser // way of saving a file does nothing inside the native apps. Instead the file // is written to the app's cache and handed to the system share sheet, where // the user can save it to Files, AirDrop it, send it to another app, etc. // The copy is left in the cache (the receiving app may read it after the // share sheet closes); the system clears the cache when it needs space. export async function saveFileInNativeApp( contents: string | Uint8Array, filename: string, dialogTitle?: string, ) { const { uri } = await Filesystem.writeFile({ // Each export gets its own folder, so a later export with the same name // cannot replace a file another app is still reading. A slash in the name // (e.g. from a budget or account name) would be read as a folder too. path: `${Date.now()}/${filename.replaceAll('/', '-')}`, recursive: true, directory: Directory.Cache, // Text is written as-is; only binary data needs the base64 round trip. ...(typeof contents === 'string' ? { data: contents, encoding: Encoding.UTF8 } : { data: await toBase64(contents) }), }); try { await Share.share({ files: [uri], dialogTitle }); } catch (error) { // Closing the share sheet without picking anything is not a failure. if (!isShareCanceled(error)) { throw error; } } } function toBase64(contents: Uint8Array): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { // The result is a data URL: "data:;base64,". const dataUrl = String(reader.result); resolve(dataUrl.slice(dataUrl.indexOf(',') + 1)); }; reader.onerror = () => reject(reader.error); // Copied because a Blob cannot be made from a view over shared memory. reader.readAsDataURL(new Blob([new Uint8Array(contents)])); }); } function isShareCanceled(error: unknown) { return ( error instanceof Error && error.message.toLowerCase().includes('cancel') ); }