I want upload image from gallery to api here is my image api
Future profileImageUser(String user_id, String image) async {
String url = 'http://bitborgtech.com/appss/api/update_profile_image';
final response = await http.post(Uri.parse(url),
body: {
'user_id': user_id,
'image': image
}
);
var convertedDataJson = jsonDecode(response.body);
return convertedDataJson;
}
Here is how i am getting user id and image to upload it through rest api
ImagePicker picker = ImagePicker();
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 myProfile = _image;
print(myProfile);
var myId = prefs.getString('user_id');
print(myId);
var rsp = await profileImageUser(
myProfile.toString(),
myId.toString(),
);
print(rsp);
if (rsp['status'] == true) {
print(rsp);
} else {
print('failed');
}
}
}
when i upload image from gallery or camera it print id and image as follow
I/flutter ( 6665): Instance of 'XFile'
I/flutter ( 6665): 111
but it also show error that user doest not exist
I/flutter ( 6665): {status: false, message: User Does Not Exists!, data: null}
kindly provide a solution.
CodePudding user response:
When you use ImagePicker to obtain an image, it uses a custom class named XFile to make things easy. To convert your image into a file, you should replace image
(which is an XFile) with this:
File(image.path);
Additionally, I am not sure if that kind of request would work. Perhaps JSON form data would be better for sending images as far as I know (like this).
CodePudding user response:
I think you prefs.getString('user_id')
is deleted from api, rest all is okay, but not getting the user id, check api.