capacitor-inappbrowser icon indicating copy to clipboard operation
capacitor-inappbrowser copied to clipboard

feat: download/show files from webview

Open josemadev opened this issue 3 months ago • 1 comments

Current problem

Enable file downloads from the webview. I make this stackblitz for test download files from a webview https://file-download-test-bqvemhjw.stackblitz.io.

Preferred solution

Is there any way to download files and open them from the webview without implementing events?

Alternative options

I used event for receive files.

InAppBrowser.addListener('messageFromWebview', (e) => {
      console.log('Message received from webview:', e);
      if (e.detail?.['type'] === 'download') {
        this.shareFile(e.detail as DownloadEvent);
      }
    });

  private async shareFile(event: DownloadEvent): Promise<void> {
    try {
      const fileName = event.name;
      const path = `${fileName}`;

      const file = await Filesystem.writeFile({
        path,
        data: event.base64,
        directory: Directory.Documents
      });

      if (!file.uri) {
      throw new Error('Could not retrieve file URI');
      }

      if (Capacitor.getPlatform() === 'ios') {
        await InAppBrowser.setUrl({ url: file.uri });
      } else {
        await FileViewer.openDocumentFromLocalPath({
          path: file.uri
        });
      }
    } catch (error) {
      console.error('Error processing file:', error);
    }
  }

We can´t use FileViewer in iOS for same reason of https://github.com/Cap-go/capacitor-inappbrowser/issues/102

Additional context

Related issue: https://github.com/Cap-go/capacitor-inappbrowser/issues/328

Before submitting

  • [x] I have read and followed the feature request guidelines.
  • [x] I have attached links to possibly related issues and discussions.

Fastest way to get this feature added

josemadev avatar Sep 09 '25 10:09 josemadev

This can helps to do it https://github.com/OutSystems/OSInAppBrowserLib-Android/pull/51/files#diff-6a724412687b4fb20afa309947c11a9b01b041a05862c9be3709ff6fb7e33466

josemadev avatar Sep 11 '25 09:09 josemadev