Flutter 颤振Dio:无法将图像上载到服务器

Flutter 颤振Dio:无法将图像上载到服务器,flutter,file-upload,dio,Flutter,File Upload,Dio,首先,我先试试《邮递员》这本书 之后,我创建了我的dio函数,像这样上传到服务器 Future<AttendanceResponse> checkOut( String timeOfCheckout, File image, String notes, ) async { try { String fileName = image.path.split('/').last; print("Image name

首先,我先试试《邮递员》这本书

之后,我创建了我的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;
    }
  }
这是摄像机的文件和路径

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中的文档,并按照它进行操作,但仍然存在此错误。

您需要将
contentType
属性添加到表单数据中。看起来是这样的,

  final formData = FormData.fromMap({
    "out": timeOfCheckout,
    "out_picture": await MultipartFile.fromFile(
        image.path, 
        filename: fileName,
        contentType: new MediaType("image", "jpeg"), // Here we add the content type!
     ),
    "out_note": notes,
    "late": 0,
  });

这是我的错,我忘了在我的dio函数中添加
token

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;
    }
  }
未来结账(
checkout的字符串时间,
文件图像,
弦乐,
)异步的{
试一试{
var fileName=image.path.split(“/”).last;
var token=wait prefHelper.getToken();
打印(“令牌-->$Token”);
变量头={
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
“授权”:“持票人$token”,
};
final formData=formData.fromMap({
“out”:checkout的时间,
“输出图片”:等待MultipartFile.fromFile(
image.path,
filename:filename,
),
“out_note”:注释,
“迟到”:0,
});
最终响应=等待dio.post(“出席/退房”,
数据:formData,选项:选项(方法:“POST”,标题:标题));
返回AttendanceResponse.fromJson(response.data);
}关于错误捕获(e){
返回e.error;
}
}

我尝试添加,但仍然不起作用,仍然收到相同的错误
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;
    }
  }