0
votes

I am trying to send an image from Flutter application to Cloudinary using Uploading with a direct call to the REST API,but getting this error:

{"error":{"message":"Invalid file parameter. Make sure your file parameter does not include '[]'"}}

I tried to encode image as an array of bytes, base64Encode but none of that worked, I followed this documentation.

Does anybody know how to encode the file so I can send it?

thanks

EDIT:

@override
  Future<void> getImageSignature(File image) async {
    return await _callWithExceptionWrap(() async {
      if (image != null) {
        DateTime dateTime = DateTime.now();
        String url = _formatUrlForUploadSignature();
        Dio dio = NetworkUtils.createDioConnection();
        debugPrint('REQUEST TO SERVER');
        Response serverResponse = await dio.post(url, data: {
          "paramsToSign": {
            'public_id': 'public_id_654',
            "timestamp": dateTime.millisecondsSinceEpoch,
            "upload_preset": "signed_preset",
            "source": "uw",
          }
        });
        debugPrint('REQUEST TO CLOUDINARY');
        String signature = serverResponse.data['signature'];
        List<int> bytes = image.readAsBytesSync();
        var base64Image = base64Encode(bytes);

        Map<String, dynamic> map = {
          'api_key': _CLOUDINARY_API_KEY,
          'public_id': 'public_id_654',
          'signature': signature,
          'source': 'uw',
          'timestamp': dateTime.millisecondsSinceEpoch,
          'upload_preset': 'signed_preset',
          'file': base64Image,
        };
        debugPrint('json : ${map}');
//      FormData formData = new FormData.fromMap(map);
        Response cloudinaryResponse = await dio.post(_CLOUDINARY_URL, data: map);
        debugPrint('*************************** Cloudinary response : ${cloudinaryResponse.data}');
      }
    });

The signature is ok, since I am not getting 401 error(signature I am receiving from the server.

here is the cloudinary url: _CLOUDINARY_URL = 'https://api.cloudinary.com/v1_1//image/upload';

1

1 Answers

1
votes

That is not correct api to use on a public client (Mobile app), you shouldn't be exposing your API_KEY and API_SECRET. Check out this package instead, which uses the correct api to upload files https://pub.dev/packages/cloudinary_public