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