File.writeFile: Not working on Android Capacitor

Heyo,

I am using the following code to download a file. This works on iOS without any issue.

On Android at least in the emulator on the localhost. However, after deploying to the server and running the app either in an emulator pointing to this, or a real device, an error message appears. Only on Android.

Do you have any idea how I can solve this? Thank you! :slight_smile:

    if (isPlatform('android')) {
        try {
            setTimeout(() => {
                File.removeFile(File.externalDataDirectory, filename)
            }, 2000)
        } catch (e) {
            console.log(e)
        }
        File.writeFile(File.externalDataDirectory, filename, content, true).then(success => {
            FileOpener.showOpenWithDialog(File.externalDataDirectory + filename, mimeType)
                .then(() => console.log('File opened'))
                .catch(e => toast.error("Fehler beim Öffnen der Datei: " + e));
        })
            .catch(e => toast.error("Fehler beim Speichern der Datei: " + e));
    }

Uncaught TypeError: Cannot read property 'then' of undefined

I gave it now a second attempt with the Capacitor’s implementation. Same issue.

    if (isPlatform('android')) {
        if (filename.includes(".json")) {
            filename = filename.replace(".json", ".xlsx");
            encodeBase64(content)
        }
        const result = await Filesystem.writeFile({
            path: filename,
            data: content,
            directory: Directory.Documents
        });

        FileOpener.showOpenWithDialog(result.uri, mimeType)
    }

I could resolve the issue. Android Capacitor does not seem to be supporting the service-worker. I removed it from React and the server. Plugins are working fine now.