我在叫Flutter Camera软件包的地点,拍照并使用pathProvider将其临时保存在我的应用程序中。现在,我想使用Dio包将此图像上传到api。我的pathprovider可以给我一个带有图像路径的字符串。通过此路径,我可以使用Flutter中的Image小部件显示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);
}
答案 0 :(得分:0)
Dio库具有一个名为UploadFileInfo
的类,可用于您的案例:
FormData formData =
new FormData.from({
"size": "auto",
"image_file": UploadFileInfo(newfile, "file_name.txt")},
);