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

7 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

Considering it is a Base64 string, which process parameter type should I choose? Unlimited length text?

 

Also, I would like to get image width and height in the code and save it to parameter as well.

How can I get them in code?

Since Base64 string might be long Unlimited length text type will be a good choice for the parameter that will be used for storing it.

 

In order to get and save image width and height first you have to create 2 process parameters of type Integer (e.g.  ImageWidthParam and ImageHeightParam). Then in Script Task you can get the values using the following code:

 

using (System.IO.Stream stream = fileItem.Read())
{
	using (Image img = Image.FromStream(stream))
	{
		Set<int>("ImageWidthParam", img.Width);
		Set<int>("ImageHeightParam", img.Height);
	}
	/* Retrieve the file content and save it to the array. */
	var content = stream.ReadToEnd();
	var encodedContent = Convert.ToBase64String(content);
	Set<string>("EncodedFile", encodedContent);
} 


Also add System.Drawing namespace to the business process the same way as you added Terrasoft.File and Terrasoft.File.Abstractions namespaces.

 

Thank you so much for the replies!

 

Only thing that doesn't work is that EncodedFile seems to be empty.

When I try to print it inside AutoGeneratedPage as a text field it shows nothing, and when I forward it in a WebService .json body to my API there is no data.

 

Process file element is fetching last 10 records from uploaded files, I tried looking up uploaded files and there are test images there, so there should be valid input to my ScriptTask.

 

Is there something about Process Parameter "EncodedFile" that I should set up differently except setting its type as Unlimited length text? Everything else I left at default values.

The problem that EncodedFile parameter is empty could be that the stream position is at the end, so when you call stream.ReadToEnd(), there's nothing left to read.
You can update a proposed code a bit to make sure that stream position is not at the end before reading from it.

 

/* Retrieve the file content and save it to the array. */
var content = stream.ReadToEnd();
var encodedContent = Convert.ToBase64String(content);
Set<string>("EncodedFile", encodedContent);
using (MemoryStream imageStream = new MemoryStream(content))
using (Image img = Image.FromStream(imageStream))
{
	Set<int>("ImageWidthParam", img.Width);
	Set<int>("ImageHeightParam", img.Height);
}

 

Also I would recommend to debug Script Task code in Visual Studio in case you have some issues to make sure that it's executing as expected.

Show all comments