0

我无法将图像上传到 rest api 我已经检查了 api 它的工作正常。

我无法将文件(图像)转换为可上传的表格,有人可以帮我吗?我已经运行了 flutter doctor -v 一切都很好 :) 这是我用来发布表单数据的代码:

Future<void> uploadAccountDetails(AccountDetailsModel details) async {
    var url = baseUrl + '/api/uploads/images';
    try {
      Dio dio = new Dio();
      FormData formData = new FormData.fromMap(
        {
          'city': details.cityName,
          'country': details.countryName,
          'residence_address': details.address,
          'dob': details.dob,
          'id_num': details.id,
          'passport_num': details.passportNo,
          'driving_license_nim': details.drivingLicNo,
          'user_id': 162,
          'postal_code': details.postalCode,
          'id_pic': await MultipartFile.fromFile(details.idPic.path,
              filename: basename(details.idPic.path)),
          'driving_license_pic':
              await MultipartFile.fromFile(details.drivingLicPic.path,
                  filename: basename(
                    details.drivingLicPic.path,
                  )), 
          'birth_certificate': await MultipartFile.fromFile(
            details.drivingLicPic.path,
            filename: basename(details.birthCertPic.path),
          ),
          'residence_permit_pic': await MultipartFile.fromFile(
            details.resPermitPic.path,
            filename: basename(details.resPermitPic.path),
          ),
          'profile_pic': await MultipartFile.fromFile(details.profilePic.path,
              filename: basename(details.profilePic.path)),
        },
      );
      print(formData);
      Response response = await dio.post(
        url,
        data: formData,
        onSendProgress: (received, total) {
          if (total != -1) {
            print((received / total * 100).toStringAsFixed(0) + "%");
          }
        },
      );
      print(response.statusCode);
      // print(response);

    } catch (e) {
      print(e);
      throw (e);
    }
  }

错误日志如下:

I/flutter (22523):DioError [DioErrorType.RESPONSE]:Http 状态错误 [500] I/flutter (22523):DioError [DioErrorType.RESPONSE]:Http 状态错误 [500]

状态错误是:

I/flutter (22523):DioError [DioErrorType.RESPONSE]:Http 状态错误 [500]

我搜索了这个错误但没有找到任何解决方案,有人可以帮助我吗?

4

1 回答 1

0

I used dio for post a file path with some other information in this way:

  Dio dio = new Dio();
  FormData formData = new FormData();
  formData.add(
  "apiKey",
  "my_api_key",
  );
  formData.add(
  "file",
  "image_path",
  );
 Response response = await dio.post(
"https://localhost",
data: formData,
onSendProgress: (int sent, int total) {
  // do something
},
).catchError((onError) {
 throw Exception('something');
});
于 2019-10-22T06:14:13.053 回答