0

Update:

After research I found that unfortunately it is impossible for now (April 2023) to work (to cut/split) binary data with PowerAutomate functions. Here is same problem described: https://poszytek.eu/en/microsoft-en/office-365-en/powerautomate-en/how-to-move-copy-infopath-attachments-in-sharepoint-online/

Autor Tomasz Poszytek used Azure function and premium connector. Tried to find free solution but seems there is no such.


Infopath XML stores embedded images in base64 string, which contains file name/type and file content. Here are two embedded images:

Here are two embedded images:

In Infopath XML this image is stored like base64 string:

<my:group4>
    <my:field18>
        <!-- jpg-image.jpg encoded in infopath form: -->
        x0lGQRQAAAABAAAAAAAAAO+aAAAOAAAAcABuAGcALQBpAG0AYQBnAGUALgB...
    </my:field18>
</my:group4>

First part contains file name and the second is encoded image. But we need to encode this data to binary and then split file name/image type and image content itself:

enter image description here

We can then use encoded image <data> part as <img> content like this: 

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAA..." />

But xml string in Infopath stores both filename and content in one line, so I need to convert it to bytes, and then read data byte-per-byte and split file name and image content.

Here I put examples of InfoPath string and C# code, that decodes it and get file name/and content strings:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
using System.Security.Cryptography;
using System.IO;


namespace getFilenameFromBase64
{
    class Program
    {

        // "jpg-image.jpg" file encoded in infopath xml
        private static string base64FromInfopath = "x0lGQRQAAAABAAAAAAAAAPBgAAAOAAAAa....";

        // "png-image.png"
        //private static string base64FromInfopath = "x0lGQRQAAAABAAAAAAAAAO+aAAAOAAAAc...";

        static void Main(string[] args)
        {
            // This function from Quazi Anis gets filename from encoded string
            // https://www.infopathdev.com/forums/p/15120/53720.aspx#53720
            Encoding _encoding = Encoding.Unicode;

            // Read base64 string and get header, filename and image content separately
            using (MemoryStream _memoryStream = new MemoryStream(Convert.FromBase64String(Program.base64FromInfopath)))
            {
                BinaryReader _theReader = new BinaryReader(_memoryStream);
                byte[] _headerData = _theReader.ReadBytes(16);
                int _fileSize = (int)_theReader.ReadUInt32();

                int _imageNameLength = (int)_theReader.ReadUInt32() * 2;
                byte[] _fileNameBytes = _theReader.ReadBytes(_imageNameLength);

                // here is file name decoded:
                string fileName = _encoding.GetString(_fileNameBytes, 0, _imageNameLength - 2);
                Console.WriteLine(fileName);                                                // result: "jpg-image.jpg"

                // and here is base64 data of pure image that we can use in html: <img src="data&colon;image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqMAAAIZCAIAAAAdrK.." />
                var imageContentInBytes = _theReader.ReadBytes(_fileSize);
                var imageContentAsString = Convert.ToBase64String(imageContentInBytes);     // result: "iVBORw0KGgoAAAANSUhEUgAAAqMAAAIZCAIAAAAdrK.."

                Console.WriteLine(imageContentAsString);
            }
        }
    }
}

Here are project sources if needed:

https://github.com/Gennady-G/ExtractImageFromInfopathXml/blob/main/getFilenameFromBase64/getFilenameFromBase64/Program.cs

Can I do something similar (make operations with bytes array) in SharePoint power automate? Or what You think I can do to decode Infopath base64 string and extract file name/type and image content from there?

I attached some InfoPath strings example here in github gists: https://gist.github.com/Gennady-G/48d740c92593e1ce8234b4a8c3a23069

Have no problem to read them from XML, but don't know how to decode and split file name and data

1 Answer 1

1

Power Platform has some base64 functions to en/de-code strings and binaries that should do the trick:

#1 https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#b

Of course you will have to split the string or just get rid of the prefix if it is always the same ("data:image/png;base64,") and maybe save the file (image) in an accessible location (onedrive, blob storage, SharePoint folder, etc)

#2 Another way that worked is in the following picture (better than 10^3 words)

PowerAutomate flow that saves a given base64 string as an image file in OneDrive

3
  • Thank You @dinos.kon! I'll try and keep You posted. Best regards, Gennady
    – Gennady G
    Commented Apr 6, 2023 at 12:55
  • Hi @dinos.kon! Thank You twice but the source string in my case is not in "data:image/png;base64," format. I can split it and convert it if it was.. It is in "x0lGQRQAAAABAAAAAAAAAPBgA" format (not sure what format is used in Infopath Xml..) Examples of these strings are here: gist.github.com/Gennady-G/48d740c92593e1ce8234b4a8c3a23069 ) So if I understand correctly I need to convert this "x0lGQRQAA" string to byte array and then back to "data:image/png;base64" string
    – Gennady G
    Commented Apr 7, 2023 at 10:01
  • Hi @dinos.kon! Updated answer. Looks there is no easy way, paid subscription needed. Thank You twice! Best regards, Gennady
    – Gennady G
    Commented Apr 12, 2023 at 13:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.