首先,我首先尝试邮递员,它的工作。
之后,我创建了dio函数,以将其上传到服务器。
Future<AttendanceResponse> checkOut(
String timeOfCheckout,
File image,
String notes,
) async {
try {
String fileName = image.path.split('/').last;
print("Image name --> $fileName");
print("Image path --> ${image.path}");
final formData = FormData.fromMap({
"out": timeOfCheckout,
"out_picture": await MultipartFile.fromFile(
image.path,
filename: fileName,
),
"out_note": notes,
"late": 0,
});
final response = await dio.post("attendances/check-out", data: formData);
return AttendanceResponse.fromJson(response.data);
} on DioError catch (e) {
return e.error;
}
}
但是出现如下错误。
Dio Response Error --> DioError [DioErrorType.RESPONSE]: Http status error [302]
这是相机的文件和路径。
I/flutter ( 2163): Image name --> scaled_32592bd5-704d-4e0a-9976-9ea94c667f7d4583818212866102164.jpg
I/flutter ( 2163): Image path --> /storage/emulated/0/Android/data/id.cometdev.bozzetto.dev/files/Pictures/scaled_32592bd5-704d-4e0a-9976-9ea94c667f7d4583818212866102164.jpg
我已经在谷歌上搜索并阅读了dio的文档并进行了跟踪,但是仍然出现此错误。
这很不好,我忘了添加token
到dio函数中。
Future<AttendanceResponse> checkOut(
String timeOfCheckout,
File image,
String notes,
) async {
try {
var fileName = image.path.split('/').last;
var token = await prefHelper.getToken();
print("Token --> $token");
var headers = {
'content-type': 'application/json',
'accept': 'application/json',
'authorization': 'Bearer $token',
};
final formData = FormData.fromMap({
"out": timeOfCheckout,
"out_picture": await MultipartFile.fromFile(
image.path,
filename: fileName,
),
"out_note": notes,
"late": 0,
});
final response = await dio.post("attendances/check-out",
data: formData, options: Options(method: "POST", headers: headers));
return AttendanceResponse.fromJson(response.data);
} on DioError catch (e) {
return e.error;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句