Flutter inAppWebView blob url download

56 views Asked by At

I'm working on a Flutter application where I'm using the InAppWebView package to display web content. In certain cases, the web content contains blob URLs that represent files that need to be downloaded.

I'm trying to implement a feature where users can download these files when they encounter blob URLs within the InAppWebView. However, I'm unsure about how to handle this scenario.

I've tried listening to the onDownloadStart event provided by InAppWebView, but I'm unsure about how to proceed from there to actually download the file represented by the blob URL.

Could someone provide guidance or a code example on how to properly handle downloading files from blob URLs within the InAppWebView in Flutter?

Thank you in advance for any assistance!

   InAppWebView(
                            initialUrlRequest:
                                URLRequest(url: WebUri(redirectionLink!)),
                            onPermissionRequest: (controller, request) async {
                              return PermissionResponse(
                                  resources: request.resources,
                                  action: PermissionResponseAction.GRANT);
                            },
                            initialSettings: InAppWebViewSettings(
                              allowsInlineMediaPlayback: true,
                              useShouldOverrideUrlLoading: true,
                              javaScriptEnabled: true,
                            ),
                            onWebViewCreated:
                                (InAppWebViewController controller) {
                              _webViewController = controller;
                              if (redirectionLink
                                  .toString()
                                  .contains('.pdf')) {
                                Navigator.pop(context);
                                launchURLtoBrowser(
                                    redirectionLink.toString());
                             }

                            },
                            onProgressChanged: (controller, progress) {
                              setState(() {
                                this.progress = progress / 100;
                              });
                            },
                            shouldOverrideUrlLoading:
                                (controller, action) async {
                              var baseUrl = env?.getBaseURl();
                              if (action.request.url
                                  .toString()
                                  .contains(baseUrl as Pattern)) {
                                return NavigationActionPolicy.ALLOW;
                              }
                              else {
                                launchUrl(
                                    Uri.parse(action.request.url.toString()),
                                    mode: LaunchMode.externalApplication);
                                return NavigationActionPolicy.CANCEL;
                              }
                            }
                          )  

getting this in the output logs [AndroidInAppWebViewController] (android) WebView ID 0 calling "onDownloadStartRequest" using {mimeType: image/png, textEncodingName: null, suggestedFilename: ea66d12b-3825-4ea7-bea0-9c3a8020023d.png, userAgent: Mozilla/5.0 (Linux; Android 7.1.2; Redmi 4 Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/119.0.6045.193 Mobile Safari/537.36, contentLength: 253801, url: blob:https://abcd.com/ea66d12b-3825-4ea7-bea0-9c3a8020023d, contentDisposition: }

0

There are 0 answers