uploadFile method

Future<Response<InlineResponse201>> uploadFile({
  1. String? channelId,
  2. String? filename,
  3. MultipartFile? files,
  4. String? channelId2,
  5. String? clientIds,
  6. CancelToken? cancelToken,
  7. Map<String, dynamic>? headers,
  8. Map<String, dynamic>? extra,
  9. ValidateStatus? validateStatus,
  10. ProgressCallback? onSendProgress,
  11. ProgressCallback? onReceiveProgress,
})

Upload a file Uploads a file that can later be attached to a post. This request can either be a multipart/form-data request with a channel_id, files and optional client_ids defined in the FormData, or it can be a request with the channel_id and filename defined as query parameters with the contents of a single file in the body of the request. Only multipart/form-data requests are supported by server versions up to and including 4.7. Server versions 4.8 and higher support both types of requests. ##### Permissions Must have `upload_file` permission.

Parameters:

  • channelId - The ID of the channel that this file will be uploaded to
  • filename - The name of the file to be uploaded
  • files - A file to be uploaded
  • channelId2 - The ID of the channel that this file will be uploaded to
  • clientIds - A unique identifier for the file that will be returned in the response
  • cancelToken - A CancelToken that can be used to cancel the operation
  • headers - Can be used to add additional headers to the request
  • extras - Can be used to add flags to the request
  • validateStatus - A ValidateStatus callback that can be used to determine request success based on the HTTP status of the response
  • onSendProgress - A ProgressCallback that can be used to get the send progress
  • onReceiveProgress - A ProgressCallback that can be used to get the receive progress

Returns a Future containing a Response with a InlineResponse201 as data Throws DioError if API call or serialization fails

Implementation

Future<Response<InlineResponse201>> uploadFile({
  String? channelId,
  String? filename,
  MultipartFile? files,
  String? channelId2,
  String? clientIds,
  CancelToken? cancelToken,
  Map<String, dynamic>? headers,
  Map<String, dynamic>? extra,
  ValidateStatus? validateStatus,
  ProgressCallback? onSendProgress,
  ProgressCallback? onReceiveProgress,
}) async {
  final _path = r'/files';
  final _options = Options(
    method: r'POST',
    headers: <String, dynamic>{
      ...?headers,
    },
    extra: <String, dynamic>{
      'secure': <Map<String, String>>[
        {
          'type': 'http',
          'scheme': 'bearer',
          'name': 'bearerAuth',
        },
      ],
      ...?extra,
    },
    contentType: 'multipart/form-data',
    validateStatus: validateStatus,
  );

  final _queryParameters = <String, dynamic>{
    if (channelId != null) r'channel_id': encodeQueryParameter(_serializers, channelId, const FullType(String)),
    if (filename != null) r'filename': encodeQueryParameter(_serializers, filename, const FullType(String)),
  };

  dynamic _bodyData;

  try {
    _bodyData = FormData.fromMap(<String, dynamic>{
      if (files != null) r'files': files,
      if (channelId2 != null) r'channel_id': encodeFormParameter(_serializers, channelId2, const FullType(String)),
      if (clientIds != null) r'client_ids': encodeFormParameter(_serializers, clientIds, const FullType(String)),
    });

  } catch(error, stackTrace) {
    throw DioError(
       requestOptions: _options.compose(
        _dio.options,
        _path,
        queryParameters: _queryParameters,
      ),
      type: DioErrorType.other,
      error: error,
    )..stackTrace = stackTrace;
  }

  final _response = await _dio.request<Object>(
    _path,
    data: _bodyData,
    options: _options,
    queryParameters: _queryParameters,
    cancelToken: cancelToken,
    onSendProgress: onSendProgress,
    onReceiveProgress: onReceiveProgress,
  );

  InlineResponse201 _responseData;

  try {
    const _responseType = FullType(InlineResponse201);
    _responseData = _serializers.deserialize(
      _response.data!,
      specifiedType: _responseType,
    ) as InlineResponse201;

  } catch (error, stackTrace) {
    throw DioError(
      requestOptions: _response.requestOptions,
      response: _response,
      type: DioErrorType.other,
      error: error,
    )..stackTrace = stackTrace;
  }

  return Response<InlineResponse201>(
    data: _responseData,
    headers: _response.headers,
    isRedirect: _response.isRedirect,
    requestOptions: _response.requestOptions,
    redirects: _response.redirects,
    statusCode: _response.statusCode,
    statusMessage: _response.statusMessage,
    extra: _response.extra,
  );
}