13

I am using flutter_webview_plugin: ^0.3.8 but I have the same problem with webview_flutter: ^0.3.13.

In webview, I want to make use of a website which triggers a file download on successful completion of a captcha. However, I complete the captcha and nothing happens, no download.

Is there something in Flutter like a webview download listener ("webview.setDownloadListener")? I only need this for Android.

If not, is there a way of downloading files from a webview in Flutter?

Code Poet
  • 2,858
  • 2
  • 18
  • 35
  • 1
    Check out this answer if you are owner of code at html and js side - https://stackoverflow.com/questions/56247542/how-to-download-create-pdf-through-webview-in-flutter/59899281#59899281 – Ratnadeep Bhattacharyya Feb 20 '20 at 05:40
  • Here did you find a solution, even I'm facing the same issue. Tried using different packages as well but no use, Need help! – Varun Aug 04 '21 at 10:57
  • No, in the end I just had to use webview natively. – Code Poet Aug 04 '21 at 20:27

3 Answers3

5

A similar issue can be found here!

You can use my plugin flutter_inappwebview, which is a Flutter plugin that allows you to add inline WebViews or open an in-app browser window and has a lot of events, methods, and options to control WebViews. It can recognize downloadable files in both Android (using setDownloadListener) and iOS platforms!

I report here the same answer that I gave to the similar issue:

To be able to recognize downloadable files, you need to set the useOnDownloadStart: true option, and then you can listen the onDownloadStart event!

Also, for example, on Android you need to add write permission inside your AndroidManifest.xml file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Then, you need to ask permission using the permission_handler plugin. Instead, to effectively download your file, you can use the flutter_downloader plugin.

Here is a complete example using http://ovh.net/files/ (in particular, the http://ovh.net/files/1Mio.dat as URL) to test the download:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterDownloader.initialize(
      debug: true // optional: set false to disable printing logs to console
  );
  await Permission.storage.request();
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  InAppWebViewController webView;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
          Expanded(
              child: InAppWebView(
            initialUrl: "http://ovh.net/files/1Mio.dat",
            initialHeaders: {},
            initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                debuggingEnabled: true,
                useOnDownloadStart: true
              ),
            ),
            onWebViewCreated: (InAppWebViewController controller) {
              webView = controller;
            },
            onLoadStart: (InAppWebViewController controller, String url) {

            },
            onLoadStop: (InAppWebViewController controller, String url) {

            },
            onDownloadStart: (controller, url) async {
              print("onDownloadStart $url");
              final taskId = await FlutterDownloader.enqueue(
                url: url,
                savedDir: (await getExternalStorageDirectory()).path,
                showNotification: true, // show download progress in status bar (for Android)
                openFileFromNotification: true, // click on notification to open downloaded file (for Android)
              );
            },
          ))
        ])),
      ),
    );
  }
}

Here, as you can see, I'm using also the path_provider plugin to get the folder where I want to save the file.

Lorenzo Pichilli
  • 2,147
  • 1
  • 21
  • 38
  • Hi @Lorenzo Pichilli, this doesn't save files to the downloads folder. How do I go about doing that? – sambam Mar 04 '21 at 12:36
  • 1
    This isn't related to the flutter_inappwebview plugin. What I wrote here was just an example. If you need to specify another directory, check the [path_provider](https://pub.dev/packages/path_provider) plugin documentation! However, it seems that the method `getDownloadsDirectory` is not supported on Android and iOS (see the [official doc](https://pub.dev/documentation/path_provider/latest/path_provider/getDownloadsDirectory.html)) – Lorenzo Pichilli Mar 04 '21 at 13:42
  • Okay got it! Thanks Lorenzo – sambam Mar 06 '21 at 19:10
2

just add this code to your AndroidManifest.xml

   <provider
            android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider"
            android:authorities="${applicationId}.flutter_downloader.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
    </provider>

and add this code to your AndroidManifest.xml

it works to me

0

it works for me

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

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

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();
                  },
Dimas
  • 5
  • 2