Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kimbrough 11 posts 141 karma points
    Jun 16, 2019 @ 12:58
    Kimbrough
    0

    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.

        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...

            if (formData.HeadshotFile != null) {
                var headshotImageCrop = _dataTypeService.GetByEditorAlias(ImageCropperPropertyEditorAlias).Where(c => c.Id == MemberHeadshotImageCropperId).FirstOrDefault();
                ImageCropperValue icv = mbr.GetImageCropperValue(nameof(formData.Headshot), headshotImageCrop, formData.HeadshotFile, _contentTypeBaseServiceProvider);
                mbr.SetValue(nameof(formData.Headshot), icv);
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft