Flutter 如何进行图片选择与上传?

来源:6-16 拍照APP开发-图片获取与图片展示【实战尝鲜】

CrazyCodeBoy

2019-12-16

// My IPv4 : 192.168.43.171 
final String phpEndPoint = 'http://192.168.43.171/phpAPI/image.php';
final String nodeEndPoint = 'http://192.168.43.171:3000/image';
File file;

 void _choose() async {
   file = await ImagePicker.pickImage(source: ImageSource.camera);
// file = await ImagePicker.pickImage(source: ImageSource.gallery);
 }

 void _upload() {
   if (file == null) return;
   String base64Image = base64Encode(file.readAsBytesSync());
   String fileName = file.path.split("/").last;

   http.post(phpEndPoint, body: {
     "image": base64Image,
     "name": fileName,
   }).then((res) {
     print(res.statusCode);
   }).catchError((err) {
     print(err);
   });
 }
写回答

1回答

CrazyCodeBoy

提问者

2019-12-16

如上。

2
0

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3277 问题

查看课程