I am reading a file using Process file system action. I have to send the base64 format of the file to an external api. How can I do that conversion using the script task?
To convert a file read via the "Process file" system action into a Base64 string within a business process, you will use a Script Task. This allows you to perform the conversion using C# and then pass the resulting string to an external API.
var files = context.Process.FindFlowElementByName("ObjectFileProcessingUserTask1") .GetPropertyValue("ObjectFiles") as ICompositeObjectList<ICompositeObject>;
if (files != null) { foreach(var file in files) { if(file.TryGetValue<EntityFileLocator>("File", out EntityFileLocator fileLocator)) { IFile fileItem = UserConnection.GetFile(fileLocator); // This now works due to the 'using' directives [11] using (System.IO.Stream stream = fileItem.Read()) { using (var memoryStream = new System.IO.MemoryStream()) { stream.CopyTo(memoryStream); byte[] content = memoryStream.ToArray(); var encodedContent = Convert.ToBase64String(content); Set<string>("EncodedFile", encodedContent); } } } } }
return true;
In the Methods tab, add the following to the Usings list:
I do wish it would return the ID, however, since it *can* create multiple files for whatever matches the filter conditions it's not always a single ID or file created.
What I typically do is do a read immediately after for attachments on the record, sorted by Created On desc, and also use the Name of the file based on the naming of the printable used. It's not ideal but has worked for me.