Introduction

This article describes how to encode a file, typically an image, into Base64 for use with web services.

The procedure described here extracts the contents of file, encodes it, then makes it available for use by other task steps using a variable.

Encoding the File

The following procedure is suitable for most major file types (PDF, JPG, PNG, Excel, Word, and so on):

Note: The function created here outputs a Base64 encoded string, not a Base64 byte array.

Tip: If you want to make the encoded file available to all tasks, perform the following procedure in a separate, isolated task. If the file is only for use in a single task, you can add these steps to a existing task.

Adding Required Functions

The following functions are required:

  1. Open the relevant task and create a task-level function.
  2. Name the function readBytes.
  3. Go to the Parameters tab and add a new parameter named file.
  4. Go to the Script tab and add the following script:

Dim inStream

Set inStream=CreateObject("ADODB.Stream")

inStream.Open

inStream.type=1

inStream.LoadFromFile file

readBytes=InStream.Read()

  1. Click OK to save and close the function.
  2. Create a second task-level function named encodeBase64.
  3. Go to the Parameters tab and add a new parameter named bytes.
  4. Go to the Script tab and add the following script:

Dim Cobj, CElem

Set CObj = CreateObject("Microsoft.XMLDOM")

Set CElem = Cobj.createElement("tmp")

CElem.DataType = "bin.base64"

CElem.NodeTypedValue = bytes

encodeBase64 = CElem.Text

  1. Click OK to save and close the function.

Adding Required Variables

A global or task-level variable, depending on your task requirements, is required to save the encoded contents in.

Create a blank global or task variableClosed Variables are created and used extensively throughout and can either have fixed values or values that are dynamically populated when a task is run. Task variables are only available for the specific task they are created for. Users with Task Administrator rights can create task variables. as required, that is, leave Current Value (global variablesClosed Variables are created and used extensively throughout and can either have fixed values or values that are dynamically populated when a task is run. Global variables are available for all tasks in this BPA Platform installation. Once created, global variables can be used by any task.) or Default Value (task variablesClosed Variables are created and used extensively throughout and can either have fixed values or values that are dynamically populated when a task is run. Task variables are only available for the specific task they are created for. Users with Task Administrator rights can create task variables.) blank.

The variable must be Type: Variant
If a task-level variable it must also be a Parameter variable, with Parameter Attributes set to In, Out
For task-level variables, Scope can be set to either Task or Task Instance depending on your task requirements

Converting the File to Base64 Encoding

Finally, we use the Run VBScriptClosed The Run VBScript tool is used to create a task step that runs a Visual Basic Script using data produced by other steps in the task. tool to take the file, convert it using the functions you have just created, and save the result to the variable for use in another tool.

  1. Add a Run VBScript step to your task.
  2. Add a meaningful Name to your task step.
  3. Go to the Main tab and add the following script:

inFile = "<folder_path_and_filename>"
'Gets the input file read
inByteArray = readBytes(inFile)
'Encode the data from the input file to Base64Encoded
base64Encoded = encodeBase64(inByteArray)
'Output to a variable

where folder_path_and_filename is the full path to the file.

  1. Drag the variable from the Task Browser to the end of your script and add <space>=<space> base64Encoded after it.

    Your script should now look like:

    Run VBScript - Convert File to base64 Encoded Variable

    The example above uses a task variable (Variables). If using a global variableClosed Variables are created and used extensively throughout and can either have fixed values or values that are dynamically populated when a task is run. Global variables are available for all tasks in this BPA Platform installation. Once created, global variables can be used by any task., it shows System.Variables instead.

  2. Click OK to save and close the step.

The empty variable is now populated and available for use in another or same task.

Tip: If using this procedure to upload documents to a web service (using the Web Service Connector tool), it may also be necessary to define mappings for the expected filename and file extension. For more information, refer to the individual web service or your API documentation.

Article originally published 03 December 2015 — Article updated 05 March 2020