Skip to content

Instantly share code, notes, and snippets.

@rohankandwal
Created April 29, 2023 20:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rohankandwal/c7dd80627fdade779d1e6154657cecf1 to your computer and use it in GitHub Desktop.
Save rohankandwal/c7dd80627fdade779d1e6154657cecf1 to your computer and use it in GitHub Desktop.
File Upload using WebView on Android
/// Read details on https://theprogrammingway.com/flutter-file-upload-using-webview-on-android/
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late final WebViewController controller;
@override
void initState() {
controller = WebViewController();
controller.loadRequest(
Uri.parse("https://theprogrammingway.com/html-file-upload/"));
addFileSelectionListener();
super.initState();
}
void addFileSelectionListener() async {
if (Platform.isAndroid) {
final androidController = controller.platform as AndroidWebViewController;
await androidController.setOnShowFileSelector(_androidFilePicker);
}
}
@override
Widget build(BuildContext context) {
return WebViewWidget(controller: controller);
}
/// Function for file selection from gallery
Future<List<String>> _androidFilePicker(FileSelectorParams params) async {
final result = await FilePicker.platform.pickFiles();
if (result != null && result.files.single.path != null) {
final file = File(result.files.single.path!);
return [file.uri.toString()];
}
return [];
}
}
@ahmedomar365
Copy link

doesn't work anymnore

@rohankandwal
Copy link
Author

@ahmedomar365 what's the issue? What error you are getting?

@ZaneCode6574
Copy link

Thank you for your code. It works well.

@maryannemm
Copy link

maryannemm commented Mar 6, 2024

it shows image selection but then closes. it doesnt get uploaded to the backend web app. mine is django. /// Read details on https://theprogrammingway.com/flutter-file-upload-using-webview-on-android/
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);

@OverRide
State createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
late final WebViewController controller;

@OverRide
void initState() {
controller = WebViewController();
controller.loadRequest(
Uri.parse("https://pythonanywhere.com/"));
controller.setJavaScriptMode(JavaScriptMode.unrestricted);

addFileSelectionListener();
super.initState();

}

void addFileSelectionListener() async {
if (Platform.isAndroid) {
final androidController = controller.platform as AndroidWebViewController;
await androidController.setOnShowFileSelector(_androidFilePicker);
}
}

@OverRide
Widget build(BuildContext context) {
return WebViewWidget(controller: controller,
);
}

/// Function for file selection from gallery
Future<List> _androidFilePicker(FileSelectorParams params) async {
final result = await FilePicker.platform.pickFiles();

if (result != null && result.files.single.path != null) {
  final file = File(result.files.single.path!);
  return [file.uri.toString()];
}
return [];

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment