I am building a flutter app, which utilizes image picker to capture or select an image from the gallery, but I am having a hard time how to POST that image to my server from the client side.
From what I gathered, I can send the local image via JSON by converting an image file to bytes and then sending it as BASE64.
import 'dart:convert';
import 'package:crypto/crypto.dart';
Future<Map> _avatarSubmit() async {
String url = api + '/api/account';
http.Response response = await http.post(Uri.encodeFull(url), headers: {
"Accept": "application/json",
"Cookie": "MYCOOKIE=" + sessionCookie2 + "; MYTOKENS=" + sessionCookie3,
"Content-type": "multipart/form-data",
}, body: {
"image": "",
});
Map content = JSON.decode(response.body);
return content;
}
My question is how to convert an image file in the device into bytes, so I can then use crypto plugin to convert it to BASE64?
Thank you in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…