uploadBytes method

Future<Response> uploadBytes({
  1. required String path,
  2. required Uint8List bytes,
  3. void sendProgress(
    1. int received,
    2. int total
    )?,
  4. List<Map<String, String>>? body,
  5. bool isPost = true,
  6. required String bytesExtension,
  7. bool loading = false,
  8. CancelToken? cancelToken,
})

upload file bytes

Implementation

Future<Response> uploadBytes(
    {required String path,
    required Uint8List bytes,
    void Function(int received, int total)? sendProgress,
    List<Map<String, String>>? body,
    bool isPost = true,
    required String bytesExtension,
    bool loading = false,
    CancelToken? cancelToken}) async {
  try {
    final FormData data = FormData.fromMap({
      "file": MultipartFile.fromBytes(bytes,
          filename:
              "${DateTime.now().microsecondsSinceEpoch}.$bytesExtension"),
    });

    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;
  } catch (err) {
    rethrow;
  }
}