Flutter 颤振Web图像选择器仅获取图像文件
我试着从FlatterWeb上获取图像,现在我正在这样做Flutter 颤振Web图像选择器仅获取图像文件,flutter,flutter-web,Flutter,Flutter Web,我试着从FlatterWeb上获取图像,现在我正在这样做 try { final html.InputElement input = html.document.createElement('input'); input ..type = 'file' ..accept = 'image/*'; input.onChange.listen((e) { if (input.files.isEmpty) return; final reader = ht
try {
final html.InputElement input = html.document.createElement('input');
input
..type = 'file'
..accept = 'image/*';
input.onChange.listen((e) {
if (input.files.isEmpty) return;
final reader = html.FileReader();
reader.readAsDataUrl(input.files[0]);
reader.onError.listen((err) => setState(() {
print(err.toString());
}));
reader.onLoad.first.then((res) {
final encoded = reader.result as String;
// remove data:image/*;base64 preambule
final stripped =
encoded.replaceFirst(RegExp(r'data:image/[^;]+;base64,'), '');
setState(() {
backgroundBytes = base64.decode(stripped);
backgroundImage = input.files[0];
metaDataBackground = input.files[0].type;
});
});
});
input.click();
} catch (e) {
print('Error background image');
}
它工作得很好,但是用户可以更改所有文件的文件类型,我必须阻止该操作
有什么方法可以做到这一点吗?您不能阻止该操作,请参阅参考
使用accept属性仅提供一种在感兴趣的文件类型中进行筛选的方法。浏览器仍然允许用户选择任何类型的文件 在
input.onChange.listen
中,您可以检查文件扩展名类型
,并警告用户未选择正确的类型