无法格式化文件以上传到 Cloudinary
Unable to format file to upload to Cloudinary
我正在尝试通过直接调用 REST API, 但出现此错误:
{"error":{"message":"Invalid file parameter. Make sure your file parameter does not include '[]'"}}
我尝试将图像编码为字节数组,base64Encode 但是 none 有效,我遵循了这个 documentation。
有人知道如何对文件进行编码以便我发送吗?
谢谢
编辑:
@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}');
}
});
签名没问题,因为我没有收到 401 错误(我从服务器收到的签名。
这里是云url:
_CLOUDINARY_URL = 'https://api.cloudinary.com/v1_1//image/upload';
在 public 客户端(移动应用程序)上使用 api 是不正确的,您不应该暴露您的 API_KEY 和 API_SECRET。请改用这个包,它使用正确的 api 上传文件 https://pub.dev/packages/cloudinary_public
Olajide 建议的软件包仅适用于未签名的上传。对于任何想要使用签名上传的人,请使用此包 https://pub.dev/packages/cloudinary_sdk
我正在尝试通过直接调用 REST API, 但出现此错误:
{"error":{"message":"Invalid file parameter. Make sure your file parameter does not include '[]'"}}
我尝试将图像编码为字节数组,base64Encode 但是 none 有效,我遵循了这个 documentation。
有人知道如何对文件进行编码以便我发送吗?
谢谢
编辑:
@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}');
}
});
签名没问题,因为我没有收到 401 错误(我从服务器收到的签名。
这里是云url: _CLOUDINARY_URL = 'https://api.cloudinary.com/v1_1//image/upload';
在 public 客户端(移动应用程序)上使用 api 是不正确的,您不应该暴露您的 API_KEY 和 API_SECRET。请改用这个包,它使用正确的 api 上传文件 https://pub.dev/packages/cloudinary_public
Olajide 建议的软件包仅适用于未签名的上传。对于任何想要使用签名上传的人,请使用此包 https://pub.dev/packages/cloudinary_sdk