upload method

Future<List<Attachment>> upload(
  1. int id,
  2. int version,
  3. Iterable<File> files
)

Implementation

Future<List<Attachment>> upload(
    int id, int version, Iterable<File> files) async {
  final data = FormData();
  for (var file in files) {
    var fileName = file.path.split('/').last;
    fileName = DateFormat('yyMMddhhmmss').format(DateTime.now()) +
        fileName.substring(fileName.lastIndexOf('.'));
    data.files.add(MapEntry(
        fileName,
        await MultipartFile.fromFile(
          file.path,
          contentType: MediaType.parse(lookupMimeType(file.path) ?? ""),
        )));
  }
  try {
    final response = await dio.post('/attachments/$id',
        queryParameters: {'version': version}, data: data);
    return List.from(response.data.map((a) => Attachment.fromJson(a)));
  } on DioException catch (e) {
    _handleError(e);
    rethrow;
  }
}