Introduction

The latest How To topics are available in the K2 Five User Guide or the K2 Cloud User Guide’s How to section.
This Knowledge Base article describes how to extract the file name from a file or image that has been uploaded through the File Attachment control.

Implementation

In the following scenario we will use View controls and Expressions to extract the file name from a file or image that has been uploaded. Follow the steps below to implement the scenario:
  1. Create an Item View with the following View controls:
    • Text Box control
    • File Attachment control (can be bound or unbound)
    • 3 Hidden Data Label controls:
      • Collection String
      • Start of Name
      • End of Name

        Image

  2. Set up the following Rule on the File Attachment control:
    • Select the Event “When the control is changed”
    • Select the Action “Transfer Data”

      Image

    • To configure the data transfer, drag the File Attachment control over to the Collection String control

      Image

    • Finish the Rule Designer
  3. Select the Start of Name control
  4. Set up the following Expression:

    Image

  5. Select the End of Name control
  6. Set up the following Expression:

    Image

  7. Select the Text Box control and set up the following Expression:

    Image

  8. Save and Run the View
  9. Attach a file or image and note how the Text Box control is populated with the file name

    Image

 

Notes

The reason why this method of data transfer and expression is necessary is because the transfer from the Attachment yields a string collection similar to:

<collection><object><fields><field name='FileName'><value>DSCN7311.JPG</value></field><field name='FilePath'><value>C:\Program Files (x86)\K2 blackpearl\K2 SmartForms Designer\Files\6f7d3cbf-4a6b-45f8-878d-1d4cd3008d4e.JPG</value></field></fields></object></collection>

 

We then used string-handling principles to determine the start and end of the name (the order of the nodes should remain constant) The above expressions should remain true for any file name.