我如何在抖动中发布带有dio的图像

时间:2020-02-19 20:09:17

标签: flutter dart dio

我想通过dio包以jpg格式将图像发送到服务器, 我该怎么办?

选择图像方法:

  void _chooseImageCamera() async {
file = await ImagePicker.pickImage(source: ImageSource.camera,imageQuality: 50);
setState(() {
  file = file;
  print(file);
});

上传图片方法:

  void _upload() async {
if (file == null) return;
String fileName = file.path.split('/').last;
Map<String, dynamic> formData = {
  "image": await MultipartFile.fromFile(file.path,filename: fileName),
};
await serverRequest().getRequest("/Information", formData).then((onValue) {
  print(json.decode(onValue));
});

有人帮助我吗?

谢谢

2 个答案:

答案 0 :(得分:0)

请尝试

       Future<dynamic> _upload() async {
            if (file == null) return;
            String fileName = file.path.split('/').last;
            Map<String, dynamic> formData = {
              "image": await MultipartFile.fromFile(file.path,filename: fileName),
            };
           return await Dio()
        .post(url,data:formData).
    then((dynamic result){
    print(result.toString());
});
}

答案 1 :(得分:0)

您也可以为此使用http

$('.myClass').validate({ ...
})


$('.myClass').submit(...

}