Skip to content

Instantly share code, notes, and snippets.

@DevKhalyd
Created July 14, 2020 15:52
Show Gist options
  • Save DevKhalyd/7092fc76c07563959ccf1ed99b89b882 to your computer and use it in GitHub Desktop.
Save DevKhalyd/7092fc76c07563959ccf1ed99b89b882 to your computer and use it in GitHub Desktop.
File Picker on Flutter Web
_startFilePicker() async {
InputElement uploadInput = FileUploadInputElement();
uploadInput.accept = '.csv';
uploadInput.click();
uploadInput.onChange.listen((e) {
// read file content as dataURL
final files = uploadInput.files;
if (files.length == 1) {
final file = files[0];
final reader = new FileReader();
reader.onLoadEnd.listen((e) {
print(e);
print('loaded: ${file.name}');
print('type: ${reader.result.runtimeType}');
print('file size = ${file.size}');
_handleResult(reader.result);
});
reader.readAsDataUrl(file);
}
});
}
@Shahidbangash
Copy link

How to convert this reader.result into meaningful data ?

@DevKhalyd
Copy link
Author

I don't remember what I want to do with this. But if you want to use a file picker you can check: https://pub.dev/packages/file_picker. This package contains support for Flutter Web. 👍

@Shahidbangash
Copy link

I needed this code to read CSV file using Flutter web.. As web does not support file.path .. I solved this issue by creating a new function which convert file content to Uint8List and then decode it utf8.

@ChaosCorex
Copy link

ChaosCorex commented Jun 22, 2022

how did u do that? i want to do the exact same thing .. can you help me? @Shahidbangash

@akshayMunotecgit
Copy link

how did u do that? i want to do the exact same thing .. can you help me? @Shahidbangash

Facing this same issue, please help if you got the solution!!

@ChaosCorex
Copy link

ChaosCorex commented Dec 29, 2022

@akshayMunotecgit you can try to edit this code to suite your needs, basically its file upload since you are using flutter web, so you cant use the local PC path, you use the bytes value, which is a list of integers that doesnt make any sense to you, well this list is actually transformed several times untill you get the data inside the csv or xlsx file

final List finalCsvContent = [];

FilePickerResult? result = (await FilePicker.platform.pickFiles(
withData: true,
type: FileType.custom,
allowedExtensions: ['csv'],
));
var file = result?.files.first.bytes; //bytes
String s = String.fromCharCodes(file!); // convert to string
var outputAsuint8List = Uint8List.fromList(s.codeUnits); // convert to Uint8List
var csvFileContentList = utf8.decode(outputAsuint8List).split('\n'); // decode Uniform text format 8 List into the content list
csvFileContentList.removeAt(0); // rounding for garbage value
csvFileContentList.forEach((element) {
List currentRow = [];
element.toString().split(",").forEach((items) {
currentRow.add(items.trim());
});
finalCsvContent.add(currentRow);
});

finalCsvContent.removeAt(finalCsvContent.length - 1); // rounding for garbage value

@akshayMunotecgit
Copy link

@akshayMunotecgit you can try to edit this code to suite your needs, basically its file upload since you are using flutter web, so you cant use the local PC path, you use the bytes value, which is a list of integers that doesnt make any sense to you, well this list is actually transformed several times untill you get the data inside the csv or xlsx file

final List finalCsvContent = [];

FilePickerResult? result = (await FilePicker.platform.pickFiles( withData: true, type: FileType.custom, allowedExtensions: ['csv'], )); var file = result?.files.first.bytes; //bytes String s = String.fromCharCodes(file!); // convert to string var outputAsuint8List = Uint8List.fromList(s.codeUnits); // convert to Uint8List var csvFileContentList = utf8.decode(outputAsuint8List).split('\n'); // decode Uniform text format 8 List into the content list csvFileContentList.removeAt(0); // rounding for garbage value csvFileContentList.forEach((element) { List currentRow = []; element.toString().split(",").forEach((items) { currentRow.add(items.trim()); }); finalCsvContent.add(currentRow); });

finalCsvContent.removeAt(finalCsvContent.length - 1); // rounding for garbage value

This does help! Thanks a lot!!

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