File Input From Web
fileinput.dart
abstract class FileInputData{ Future<List<int>> getBinaryData(); } abstract class FileInput { Future<List<FileInputData>> getFiles(); } abstract class FileInputBuilder { FileInput create(); }
fileinput_web.dart
import 'dart:html' as html; import 'dart:typed_data'; import './fileinput.dart'; import 'dart:async'; class FileInputDataWeb extends FileInputData { html.File file; FileInputDataWeb(this.file); Future<List<int>> getBinaryData() async { Completer completer = new Completer<List<int>>(); html.FileReader reader = new html.FileReader(); reader.onLoad.listen((event) { completer.complete(reader.result); }); reader.onError.listen((event) { completer.completeError(event); }); reader.readAsArrayBuffer(this.file); return completer.future; } } class FileInputBuilderWeb extends FileInputBuilder{ FileInput create() { return FileInputWeb(); } } class FileInputWeb implements FileInput { @override Future<List<FileInputData>> getFiles(){ print(".."); var completr = Completer<List<FileInputData>>(); try { html.InputElement elm = html.document.createElement('input'); elm.type = 'file'; elm.onChange.listen((event) { print("onchange 1"); var data = <FileInputData>[]; for(var f in elm.files){ // todo data.add(FileInputDataWeb(f)); } completr.complete(data); }); elm.onAbort.listen((event) { print("abort"); }); elm.click(); } catch(e) { print("anything wrong ${3}"); completr.completeError(e); } return completr.future; } } var inputFile = FileInputBuilderWeb().create();
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
File Input From Web
fileinput.dart
fileinput_web.dart