Skip to content

Knowledge Base

White Paper Libary

VB Script for encoding file contents into Base64 Encoded (for use in conjunction with the Web Service Connector Tool)

Summary

Some web services allow users to upload documents and document text using Base64 encoding.

The VB Script described below will extract the contents of a file on disk (most major file types will work with this including Excel, Word, PDF, JPG etc) and then encode those contents; the resulting converted data can then be attached using a Fixed/Dynamic Function to the relevant element within the web service call.

Instructions

Note: This function outputs a Base64 encoded string and not a Base64 byte array.
  1. Within the task create a task level Function called readBytes and add a parameter to the function called file. Insert the syntax to the Script tab as displayed below:
    Dim inStream
    Set inStream=CreateObject("ADODB.Stream")
    inStream.Open
    inStream.type=1
    inStream.LoadFromFile file
    readBytes=InStream.Read()
    

    1

  2. Create a second task level Function called encodeBase64; this needs a parameter called bytes to be added. The syntax for this function is as follows:
    Dim Cobj, CElem
    Set CObj = CreateObject("Microsoft.XMLDOM")
    Set CElem = Cobj.createElement("tmp")
    CElem.DataType = "bin.base64"
    CElem.NodeTypedValue = bytes
    encodeBase64 = CElem.Text
    

    2
    A task or global Variable is also required to accommodate the encoded output so that we can attach this to a Fixed/Dynamic Function within the Web Service Connector Tool step. The example below displays use of a task variable using the default name “variable1” – this has been set as a Parameter type variable with Task as the scope, although it can also be set as Task Instance.

    Variable

  3. The final step is to add a Run VB Script step to the task and add the following script:
    inFile = "C:\any_file_name.doc"
    'Gets the input file read
    inByteArray = readBytes(inFile)
    'Encode the data from the input file to Base64Encoded
    base64Encoded = encodeBase64(inByteArray)
    'Output to a variable
    Variables("variable1") = base64Encoded
    

    3
    Replace the C:\ any_file_name.doc value with the file name that you want to use and substitute variable1 with the name of the variable that you have created (if you have amended the name from the default value).

    This step will need to be placed before the Web Service Connector Tool step in the task in order to populate the variable with the encoded data from the selected file. Within the Web Service Connector Tool step, the variable can then be added to the Value tab of a Fixed/Dynamic Function and mapped to the relevant element that requires the encoded string.

Each web service will have its own process for the upload of document attachments and text, but in most cases it will also be necessary to define mappings containing the expected file extension and file name. Check the individual web service or API documentation for instructions.