How to upload files at entry creation with the Upload Plugin?

I tried to do what you suggested, but according to the docs, you need these fields in order to link an upload with an entry

I couldn’t figure out what exactly is each field, so I went the “Upload files at entry creation” route and managed to make it work.

Instead of your usual HTTP Content-Type Json Body, you need a FormData, your json body goes into the FormData as the “value” of a “data” key. Then, each file is uploaded with an specially formatted key. The pattern is 'files.MY_COMPONENT.MY_FIELD, so let’s say you have a ContentType with a “name” and a “album” field, you need to upload the file with the key files.album, and the value is the file bytes.

My working example in flutter


  @override
  Future<int> createOrUpdateAnswer(
      AnswerModel myAnswer) async {
    final _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{};
    final _headers = <String, dynamic>{};
    final _data = FormData();
    _data.fields.add(MapEntry(
      'data',
      jsonEncode(checklist),
    ));
    for (int i = 0; i < (myAnswer.answers?.length ?? 0); i++) {
      final answer = myAnswer.answers![i];
      for (final media in answer.mediaPaths) {
        final file = File(media);
        _data.files.add(MapEntry(
          'files.answers[$i].medias',
          await MultipartFile.fromFile(file.path,
              contentType: MediaType('application', 'octet-stream'),
              filename: basename(file.path)),
        ));
      }
    }

    final _result = await _dio.fetch<int>(_setStreamType<int>(Options(
      method: 'POST',
      headers: _headers,
      extra: _extra,
      contentType: 'multipart/form-data',
    )
        .compose(
          _dio.options,
          '/answers',
          queryParameters: queryParameters,
          data: _data,
        )
        .copyWith(
            baseUrl: _combineBaseUrls(
          _dio.options.baseUrl,
          baseUrl,
        ))));
    final value = _result.data!;
    return value;
  }