upload method

  1. @override
Future<ParseResponse> upload({
  1. ProgressCallback? progressCallback,
})
override

Uploads a file to Parse Server

Implementation

@override
Future<ParseResponse> upload({ProgressCallback? progressCallback}) async {
  if (saved) {
    //Creates a Fake Response to return the correct result
    final Map<String, String> response = <String, String>{
      'url': url!,
      'name': name
    };
    return handleResponse<ParseFile>(
        this,
        ParseNetworkResponse(data: json.encode(response), statusCode: 201),
        ParseApiRQ.upload,
        _debug,
        parseClassName);
  }

  progressCallback ??= _progressCallback;

  _cancelToken = CancelToken();

  final Map<String, String> headers = <String, String>{
    HttpHeaders.contentTypeHeader:
        lookupMimeType(file!.path) ?? 'application/octet-stream',
    HttpHeaders.contentLengthHeader: '${file!.lengthSync()}',
  };

  try {
    final String uri = ParseCoreData().serverUrl + _path;
    final ParseNetworkResponse response = await _client.postBytes(
      uri,
      options: ParseNetworkOptions(headers: headers),
      data: file!.openRead(),
      onSendProgress: progressCallback,
      cancelToken: _cancelToken,
    );
    if (response.statusCode == 201) {
      final Map<String, dynamic> map = json.decode(response.data);
      url = map['url'].toString();
      name = map['name'].toString();
    }

    return handleResponse<ParseFile>(
        this, response, ParseApiRQ.upload, _debug, parseClassName);
  } on Exception catch (e) {
    return handleException(e, ParseApiRQ.upload, _debug, parseClassName);
  }
}