我正在使用cloudinary在我的ASP.NET CORE 3.1应用程序中上载图像,当我调用_cloudinary.Upload(uploadParams)时,它将引发如下异常:
System.Exception: Failed to deserialize response with status code: Redirect --->
Newtonsoft.Json.JsonReaderException: Error reading JToken from JsonReader. Path '', line 0, position 0.
at Newtonsoft.Json.Linq.JToken.ReadFrom(JsonReader reader, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JToken
.Parse(String json, JsonLoadSettings settings) at Newtonsoft.Json.Linq.JToken.Parse(String json)
at CloudinaryDotNet.ApiShared.CreateResultFromString[T](String s, HttpStatusCode statusCode)
--- End of inner exception stack trace
以下是我的控制器的代码:
[HttpPost]
public async Task<IActionResult> AddPhotoForUser(int userId, [FromForm]PhotoForCreationDto photoForCreationDto)
{
if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
{
return Unauthorized();
}
var userFromRepo = await _repo.GetUser(userId);
var file = photoForCreationDto.File;
var uploadResult = new ImageUploadResult();
if (file.Length > 0)
{
using(var stream = file.OpenReadStream())
{
var uploadParams = new ImageUploadParams()
{
File = new FileDescription(file.Name, stream),
Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
};
uploadResult = _cloudinary.Upload(uploadParams);
}
}
photoForCreationDto.Url = uploadResult.Uri.ToString();
photoForCreationDto.PublicId = uploadResult.PublicId;
var photo = _mapper.Map<Photo>(photoForCreationDto);
if (!userFromRepo.Photos.Any(u => u.IsMain))
{
photo.IsMain = true;
}
userFromRepo.Photos.Add(photo);
if (await _repo.SaveAll())
{
var photoToReturn = _mapper.Map<PhotoForReturnDto>(photo);
return CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoToReturn);
}
return BadRequest("Could not add the photo");
}
我在任何地方都找不到解决方案,所以最终在此处发布了该问题,请帮助我。
我有类似的问题,但是错误是被禁止的,仅当我从cli运行应用程序时才会发生,在调试器中一切正常。https://github.com/cloudinary/CloudinaryDotNet/issues/223