我正在使用以下函数将文件从我的应用程序上传到服务器:
void _upload(File file) {
if (file == null) return;
setState(() {
_textoInfo = "subiendofotoalservidor".tr();
});
String base64Image = base64Encode(file.readAsBytesSync());
String fileName = file.path.split("/").last;
http.post(Uri.parse(phpEndPoint), body: {
"image": base64Image,
"name": fileName,
}).then((res) async {
print("respuesta servidor ${res.body}");
setState(() {
_textoInfo = "fotoperfilactualizada".tr();
miImagen = fileName;
});
print("foto subida ${miImagen}");
_cambiarFotoAdmin(miImagen, widget.administrador.email, context);
// cambiarImagenUsuario(fileName);
}).catchError((err) {
});
http.post(Uri.parse(phpEndPointFoto), body: {
"foto":fileName,
"id": miId,
}).then((res) async {
// cambiarImagenUsuario(fileName);
Provider.of<UsuarioProvider>(context, listen: false).cambiarImagenAdministrador(fileName);
}).catchError((err) {
});
}
现在,在我的新应用程序中,我在Web浏览器上执行Ffltter项目,IMAGE_PICKER不是从设备库中拾取图像,而是在计算机文件系统上拾取图像,拾取的图像来自类型Uin8List.
如何将Function_Upload(文件文件)改为_Upload(Uin8List文件)?
编辑
这里有用于 Select 图像的函数:
Future<void> _openImagePickerGallery() async {
if(!kIsWeb){
final XFile? pickedImage =
await _picker.pickImage(source: ImageSource.gallery);
if (pickedImage != null) {
setState(() {
_image = File(pickedImage.path);
});
}
}
else if(kIsWeb){
final XFile? pickedImage =
await _picker.pickImage(source: ImageSource.gallery);
if (pickedImage != null) {
var f = await pickedImage.readAsBytes();
setState(() {
hayFoto = true;
webImage = f;
});
// _image = File(pickedImage.path);
}
}
}