Copied to clipboard

Flag this post as spam?

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


  • Tom Newt 28 posts 182 karma points
    Jun 21, 2023 @ 20:10
    Tom Newt
    0

    Access Images from Forms File Upload in Custom Workflow

    I've created a custom forms workflow to save submissions for a particular form to an external database. One of the columns in that database is a user icon in Base64 string format. I followed the steps to create the workflow type and all non-images fields save correctly. The problem is the image file upload field. When I get the value for the icon image field (using context.Record.GetRecordFieldByAlias("myalias")) I see that it's the media path "/media/forms/upload/form_227751f3-6756-401a-a5af-ec7c458fae1f/8451990e-d83c-4aef-819e-8bacee1933f5/chickadee.png". How do I get that media item using that path so that I can save the icon image to the external database?

  • Tom Newt 28 posts 182 karma points
    Jun 22, 2023 @ 18:05
    Tom Newt
    101

    I got too deep into trying to do this through umbraco utilities that I missed the easy solution right in front of me. I have the relative URI of the image which is stored in wwwroot so I was able to use that to do regular file I/O and convert that image to base64.

  • Ayalatee 5 posts 75 karma points
    Oct 23, 2023 @ 03:22
    Ayalatee
    0

    can you share your code here?

  • Tom Newt 28 posts 182 karma points
    Oct 23, 2023 @ 13:27
    Tom Newt
    1

    Below is the function I use to process the images from the form, where path is context.Record.GetRecordFieldByAlias("imageAlias").ValuesAsString().

    // from https://devblogs.microsoft.com/dotnet/net-core-image-processing/
    public string ResizeAndSaveFormFileImage(string path)
    {
        string wwwrootPath = _environment.WebRootPath;
    
        string fullPath = Path.Combine(wwwrootPath, path);
    
        Uri uri = new Uri(wwwrootPath + path);
    
        string test = uri.AbsolutePath;
    
        int maxWidth = 800;
        int maxHeight = 600;
        Image image = Image.FromFile(test);
    
        int width, height;
    
        // only resize if the image is too large
        if (image.Width >= maxWidth || image.Height > maxHeight)
        {
            if (image.Width > image.Height)
            {
                width = maxWidth;
                height = Convert.ToInt32(image.Height * maxWidth / (double)image.Width);
            }
            else
            {
                width = Convert.ToInt32(image.Width * maxHeight / (double)image.Height);
                height = maxHeight;
            }
        }
        else
        {
            width = image.Width;
            height = image.Height;
        }
    
        var resized = new Bitmap(image, width, height);
    
        string base64String = string.Empty;
    
        using (MemoryStream memoryStream = new())
        {
            resized.Save(memoryStream, ImageFormat.Jpeg);
            var bytes = new byte[memoryStream.Length - 1];
            memoryStream.Position = 0;
            memoryStream.Read(bytes, 0, bytes.Length);
            base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
        }
    
        return base64String;
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft