【发布时间】:2021-11-30 20:25:34
【问题描述】:
想从画廊或相机上传图像到 api 这是我的图像 api
Future profileImageUser(String user_id, String image) async {
String url = 'api';
final response = await http.post(Uri.parse(url),
body: {
'user_id': user_id,
'image': image
}
);
var convertedDataJson = jsonDecode(response.body);
return convertedDataJson;
}
这是我如何获取用户 ID 和图像以通过 rest api 上传它。
Future _imgFromCamera() async {
final image = await ImagePicker().pickImage(source: ImageSource.camera);
setState(() {
_image = image;
final bytes = File(_image!.path).readAsBytesSync();
String img64 = base64Encode(bytes);
print(img64);
});
SharedPreferences prefs = await SharedPreferences.getInstance();
if (_formKey.currentState!.validate()) {
var myId = prefs.getString('user_id');
print(myId);
var myProfile = _image;
print(myProfile);
var rsp = await profileImageUser(
myId.toString(),
myProfile.toString(),
);
print(rsp);
if (rsp['status'] == true) {
print(rsp);
} else {
print('failed');
}
}
当我从画廊或相机上传图片时,它会打印如下的 id 和图片
I/flutter ( 6665): Instance of 'XFile'
I/flutter ( 6665): 111
但它也显示以下错误
{status: 400, message: There is some trouble to proceed your action!, data: null}
这是我的邮递员 api 参数
键:user_id 价值:76 关键:图像 值:data:image/jpeg;base64,(和其他数据)
任何解决方案。
【问题讨论】:
标签: flutter api http dart flutter-dependencies