gpt4 book ai didi

dart - 如何在flutter中使用JSON通过post发送图像?

转载 作者:IT老高 更新时间:2023-10-28 12:44:19 25 4
gpt4 key购买 nike

我正在构建一个 flutter 应用程序,它利用 image picker从图库中捕获或选择图像,但我很难从客户端将该图像发布到我的服务器。

根据我收集的信息,我可以通过 JSON 发送本地镜像,方法是将图像文件转换为字节,然后将其作为 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;
}

我的问题是如何将设备中的图像文件转换为字节,这样我就可以使用 crypto将其转换为 BASE64 的插件?

提前谢谢你。

最佳答案

作为 image picker插件提供了图片的文件路径,你可以使用 dart:io 中的 File 类加载图片,使用 dart:convert 中的 BASE64 将其转换为 BASE64 字符串。

你可以这样做:

import 'dart:io';
import 'dart:convert';

File imageFile = new File(imageFilePath);
List<int> imageBytes = imageFile.readAsBytesSync();
String base64Image = BASE64.encode(imageBytes);

希望这有帮助!

关于dart - 如何在flutter中使用JSON通过post发送图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47027418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com