public PhotoUploadResult AddPhoto(IFormFile file) { var uploadResults = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams { File = new FileDescription(file.FileName, stream), Transformation = new Transformation().Height(500).Width(500).Crop("fill").Gravity("face") }; uploadResults = _cloudinary.Upload(uploadParams); } } if (uploadResults.Error != null) { throw new Exception(uploadResults.Error.Message); } var response = new PhotoUploadResult { PublicId = uploadResults.PublicId, Url = uploadResults.SecureUri.AbsoluteUri }; return(response); }
public async Task <PhotoUploadResult> AddPhoto(IFormFile file) { PhotoUploadResult res = null; if (file.Length > 0) { await using var stream = file.OpenReadStream(); var uploadParams = new ImageUploadParams { File = new FileDescription(file.FileName, stream), Transformation = new Transformation().Height(500).Width(500).Crop("fill") }; var uploadResult = await _cloudinary.UploadAsync(uploadParams); if (uploadResult.Error != null) { throw new Exception(uploadResult.Error.Message); } res = new PhotoUploadResult { PublicId = uploadResult.PublicId, Url = uploadResult.SecureUrl.ToString(), }; } return(res); }