uploadFile method

Future<Response> uploadFile({
  1. required String path,
  2. required String filePath,
  3. bool isPost = true,
  4. void sendProgress(
    1. int received,
    2. int total
    )?,
  5. List<Map<String, String>>? body,
  6. CancelToken? cancelToken,
})

upload file

Implementation

Future<Response> uploadFile({
  required String path,
  required String filePath,
  bool isPost = true,
  void Function(int received, int total)? sendProgress,
  List<Map<String, String>>? body,
  CancelToken? cancelToken,
}) async {
  final File file = File(filePath);
  final fileName = basename(file.path);
  final FormData data = FormData.fromMap({
    "file": await MultipartFile.fromFile(
      file.path,
      filename: fileName,
    ),
  });

  if (body != null) {
    final x = body.map((e) => MapEntry(e.keys.first, e.values.first));
    data.fields.addAll(x);
  }
  late Response response;
  if (isPost) {
    response = await _dio.post(path,
        data: data, onSendProgress: sendProgress, cancelToken: cancelToken);
  } else {
    response = await _dio.patch(path,
        data: data, onSendProgress: sendProgress, cancelToken: cancelToken);
  }

  _throwIfNoSuccess(response);
  return response;
}