After finding this solution to enable members to upload images in v8 using the fileUpload dataType, I soon realized the images were being save but only with the file path and not the crops for that field.
After beating my head against the wall for a couple of days I came up with the solution below and it works for me. So if anyone else runs into the same problem this is one way to do it but I want to know if anyone has an easier solution (i.e. fewer lines using the API) to save the uploaded file and the crops.
static string _imageCropperEditorAlias = "Umbraco.ImageCropper";
/// <summary>
/// Saves a HttpPostedFileBase to the file sytem and returns an imageCropperValue for the given ImageCropper dataType
/// </summary>
public static ImageCropperValue GetImageCropperValue(this IMember member, string propertyAlias, IDataType imageCropperDataType, HttpPostedFileBase postedFile, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, decimal leftFocalPoint = ImageCropper.DefaultLeftFocalPoint, decimal topFocalPoint = ImageCropper.DefaultTopFocalPoint)
{
if (string.IsNullOrWhiteSpace(propertyAlias))
throw new ArgumentNullException(nameof(propertyAlias));
if (imageCropperDataType == null)
throw new ArgumentNullException(nameof(imageCropperDataType));
else if (imageCropperDataType.EditorAlias != _imageCropperEditorAlias)
throw new ArgumentException($"Invalid Datatype: {nameof(imageCropperDataType)} must be of type {_imageCropperEditorAlias}.");
if (postedFile == null)
throw new ArgumentNullException(nameof(postedFile));
if (contentTypeBaseServiceProvider == null)
throw new ArgumentNullException(nameof(contentTypeBaseServiceProvider));
ImageCropperValue icv = null;
// save the file to disk
member.SetValue(contentTypeBaseServiceProvider, propertyAlias, postedFile.FileName, postedFile.InputStream);
string src;
if (!string.IsNullOrWhiteSpace(src = member.GetValue(propertyAlias)?.ToString()))
{
ImageCropperConfiguration config = (ImageCropperConfiguration)imageCropperDataType.Configuration;
List<ImageCropperValue.ImageCropperCrop> imageCropperCrops = new List<ImageCropperValue.ImageCropperCrop>();
foreach (var crop in config.Crops)
imageCropperCrops.Add(new ImageCropperValue.ImageCropperCrop() { Alias = crop.Alias, Height = crop.Height, Width = crop.Width });
icv = new ImageCropperValue()
{
Src = src,
Crops = imageCropperCrops,
FocalPoint = new
FocalPoint = new ImageCropperValue.ImageCropperFocalPoint() { Left = leftFocalPoint, Top = topFocalPoint }
};
}
return icv;
}
And I call the method like this from my controller...
Save ImageCropperValues on fileUpload w/ API
After finding this solution to enable members to upload images in v8 using the fileUpload dataType, I soon realized the images were being save but only with the file path and not the crops for that field.
After beating my head against the wall for a couple of days I came up with the solution below and it works for me. So if anyone else runs into the same problem this is one way to do it but I want to know if anyone has an easier solution (i.e. fewer lines using the API) to save the uploaded file and the crops.
And I call the method like this from my controller...
is working on a reply...