how to download/create pdf through webview in flutter

后端 未结 2 1134
孤独总比滥情好
孤独总比滥情好 2020-12-11 04:02

I have created a webviewscaffold but can\'t download anything from it while browsing from the webview, I made but when I click the button it does nothing. I don\'t know wher

2条回答
  •  攒了一身酷
    2020-12-11 04:50

    If you are the owner of html page, you can check workaround that works for me. In source of your html page add onclick function for specific resource links:

    function downloadpdf() {
          var currentHref = window.location.href;
          window.history.pushState(null, null, '/app/somepdf.pdf');
          setTimeout(() => window.location.replace(currentHref), 1000);
    }
    

    In Flutter code add listener:

    StreamSubscription _onWebViewUrlChanged;
    _onWebViewUrlChanged =
        FlutterWebviewPlugin().onUrlChanged.listen((String url) {
          if (url.contains('.pdf')) {
            launchURL(url);
          }
    });
    

    launchURL will open predefined url in external browser window. So you can download pdf/etc from flutter_webview_plugin.

    You should add some your app-specific js/flutter magic

提交回复
热议问题