Convert process file output to base64

Hi Community,

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?

Any code/resource would be really helpful. TIA

Like 0

Like

1 comments

Hello, 

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:

  • Terrasoft.File
  • Terrasoft.File.Abstractions
     

     

Show all comments