
Announcements
Greetings!
I have an old Infopath system stored in SharePoint. Now I'm trying to use Power Automate to extract embedded images from there. I found how to get text part which represents image inside. So for example "x0lGQRAAA...." here is image header and image content, as I understand:
But here I stucked. I know how to create html file but cannot convert infopath encoded string to html:
Tried to use this string like this, but this does not work:
As I understand there is encoded header in this string, this is not clear file content.
Also tried to use base64ToBinary conversion and then extract content like described here
outputs('Compose_base64ToBinary')?['body/$content']
But it also does not work, base64ToBinary has same string in 'content':
How to convert this infopath string to something I can work. Or how to decode header and image content (some images could have *.png type, and other *.jpg, etc.)
Update:
So 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.
We can use encoded image part in html as image content like this:
<img src="data%3Aimage/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAA..." />
But xml string in Infopath stores both filename and content in one line, so I need to read some bytes 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:
https://gist.github.com/Gennady-G/48d740c92593e1ce8234b4a8c3a23069
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 base64 string and extract file name/type and image content from there?