【发布时间】:2019-11-30 09:43:13
【问题描述】:
我正在调用 Flutter Camera 包,拍照并使用 pathProvider 将其临时保存在我的应用程序中。现在我想使用 Dio 包将此图像上传到 api。我的路径提供者能够给我一个带有图像路径的字符串。通过这条路径,我可以使用 Flutter 中的 Image 小部件显示图像。但是我无法将其发送到 api。
processImage(file) async {
print(file);
// /var/mobile/Containers/Data/Application/7915E945-4582-4586-8062-F73537283283/Library/Caches/2019-07-22 13:35:36.243335.png
var newfile = File(file);
Dio dio = new Dio();
FormData formData =
new FormData.from({"size": "auto", "image_file": newfile});
try {
final response = await dio.post(
"YYYYYYYYYYYY",
data: formData,
options: Options(
headers: {'X-Api-Key': 'XXXXXXX'},
),
onSendProgress: (int sent, int total) {
print("send $sent / $total");
},
onReceiveProgress: (int sent, int total) {
print("receive $sent / $total");
},
);
} on DioError catch (e) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx and is also not 304.
print(e);
}
// print(response);
}
【问题讨论】:
-
not able to send it to an api是什么意思?有什么错误吗? -
我得到一个 400 意味着“输入文件无法处理”。 image_file 需要是一个字符串($binary)
-
服务器期待什么?是否期待
bas64字符串? -
不只是 FormData 中的文件。我想我可以将其转换为 base64 并将其发送到那里。但我认为这不是必需的