Encoding image to b64 in Script Task

So i'm making a simple Creatio app where I want to upload an image, encode it in base64 format, and pass it via WebService to my application.

 

I made a simple page with a button that calls business process where WebService is called.

I also added attachment component, uploaded a few images, and can successfully access the last image in my business process via formulas (get it's name, info ect.)

 

I don't have much experience in C#, so I'm kindly asking if someone could give me an example code how to read the image in Script Task named Base64 Encoder, after it's been fetched in "ImageReader" task, and return/forward encoded B64 image to "API CALL" task.

 

Thanks in advance!

Like 0

Like

3 comments

Hello,

 

First you should add Process file element to your business process where you can get the file and keep it for further usage in the process.

Then in Script Task you can retrieve that file and apply encoding. You can use the following code as an example

 

var files = context.Process.FindFlowElementByName("ObjectFileProcessingUserTask1").GetPropertyValue("ObjectFiles") as ICompositeObjectList<ICompositeObject>;
foreach(var file in files)
{
	if(file.TryGetValue<EntityFileLocator>("File", out EntityFileLocator fileLocator))
	{
		IFile fileItem = UserConnection.GetFile(fileLocator);
		using (System.IO.Stream stream = fileItem.Read())
		{
			/* Retrieve the file content and save it to the array. */
			var content = stream.ReadToEnd();
			var encodedContent = Convert.ToBase64String(content);
			Set<string>("EncodedFile", encodedContent);
		}
	}
}

 

Here ObjectFileProcessingUserTask1 is the name of Process file element in the business process.

Also don't forget to include Terrasoft.File and Terrasoft.File.Abstractions namespaces to the business process. In order to do that open the business process designer, go to the Methods tab and add the namespaces to the Usings list.

You can find more information about file processing in the articles:
https://academy.creatio.com/docs/8.x/no-code-customization/bpm-tools/process-elements-reference/system-actions/process-file-element
https://academy.creatio.com/docs/8.x/dev/development-on-creatio-platfor…

Thank you so much for the response.

I have successfully compiled my BusinessProcess, but how do I access 'EncodedFile' in my other tasks?

For example, I want to display it inside Auto-generated page, but I don't see my ScriptTask among my Process Elements in Formula window.

Please have a look at the “Process parameters” tab, as the result of the script execution is written to the “EncodedFile” process parameter

Show all comments