upload method

Future<Map<String, dynamic>> upload(
  1. String url, {
  2. required FileUploadingType type,
  3. required List<List<int>> fileBytes,
  4. String? fileKey,
  5. String? fileSuffix,
  6. Map<String, dynamic>? params,
  7. dynamic onSendProgress(
    1. int count,
    2. int total
    )?,
})

File upload.

Implementation

Future<Map<String, dynamic>> upload(String url, {
  required FileUploadingType type,
  required List<List<int>> fileBytes,
  String? fileKey,
  String? fileSuffix,
  Map<String, dynamic>? params,
  Function(int count, int total)? onSendProgress,
}) async {
  if (fileBytes.isEmpty) {
    return Future.value({'error': 'Error with empty file bytes.'});
  }

  /// Parameter assembling.
  fileKey = fileKey ?? http.config.keys.requestFileKey;
  fileSuffix = fileSuffix ?? http.config.keys.requestFileSuffix;
  Map<String, dynamic> parameters = params ?? {};
  switch (type) {
    case FileUploadingType.formData:
      parameters[fileKey] = MultipartFile.fromBytes(fileBytes.first);
      parameters['name'] = '$fileKey.$fileSuffix';
      break;
    case FileUploadingType.multipleFileData:
      int index = 0;
      parameters[fileKey] = fileBytes.map((e) {
        return MultipartFile.fromBytes(
          e,
          filename: '${fileKey}_$index.$fileSuffix',
        );
      }).toList();
      break;
  }

  /// Do request.
  url = http.config.apiDomain + url;
  final data = FormData.fromMap(parameters);
  final response = await http.dio.post(
    url,
    data: data,
    onSendProgress: onSendProgress,
  );
  if (response.statusCode != 200) {
    return Future<Map<String, dynamic>>.value(
      {"error": 'Error with Http Code: ${response.statusCode}'},
    );
  }
  if (null == response.data) {
    return Future<Map<String, dynamic>>.value(
      {"error": 'Error data: ${response.data}'},
    );
  }
  return response.data;
}