'Download Files in Flutter InAppWebview

I am Using the flutter_webview_plugin Package for displaying Web Page. On the Webpage, we had a feature to download files. the logic for the Downloading File is Similar to the below code

 filename = "my-file.txt"
 content = 'any string generated by django'
 response = HttpResponse(content, content_type='text/plain')
 response['Content-Disposition'] = 'attachment;filename={0}'.format(filename) 
 print(response)
 return response

It is working fine when it is called directly in the browser but had no effect when it comes to Flutter Webview. Is there anything to be done manually to process it? Even I am open to change the Package. I tried flutter_inappwebview also but no difference. I don't want to Launch an URL to any browser I just want to download directly from the application. is it possible to do so?



Solution 1:[1]

I suggest you try this package, flutter_inappwebview, for better performance:

  final GlobalKey webViewKey = GlobalKey();

  InAppWebViewController? webViewController;
  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
      ),
      android: AndroidInAppWebViewOptions(
        useHybridComposition: true,
      ),
      ios: IOSInAppWebViewOptions(
        allowsInlineMediaPlayback: true,
      ));

...

                      InAppWebView(
                        key: webViewKey,
                        initialUrlRequest:
                        URLRequest(url: Uri.parse("https://inappwebview.dev/")),
                        initialOptions: options,

Solution 2:[2]

it works for me

require plugin https://pub.dev/packages/url_launcher

add this code to your project to download file from flutter webview

InAppWebView(
            // ....
            onDownloadStart: (controller, url,) async {
              // print("onDownloadStart $url");
              final String _url_files = "$url";
              void _launchURL_files() async =>
                  await canLaunch(_url_files) ? await launch(_url_files) : throw 'Could not launch $_url_files';
              _launchURL_files();
            },
          ),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jim
Solution 2 Dimas