How to extract the Filename from an uploaded File or Image

  • 15 February 2022
  • 0 replies
  • 363 views

Badge +6
 

How to extract the Filename from an uploaded File or Image

KB001430

PRODUCT
K2 smartforms
LEGACY/ARCHIVED CONTENT
This article has been archived, and/or refers to legacy products, components or features. The content in this article is offered "as is" and will no longer be updated. Archived content is provided for reference purposes only. This content does not infer that the product, component or feature is supported, or that the product, component or feature will continue to function as described herein.

 

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 Filename from an uploaded File or Image.

Audience
  • Developers
  • Form Designers
Required Knowledge
  • Expression Builder
  • Views
  • Controls

 

 

Example Scenario

In the scenario below we will set up expressions to extract the name of a file. The same principle can be used to extract the name of an image by using the Image Attachment control.

Follow the steps below to set up the scenario:

  1. Create a View that contains the following:
      • Text Box control
      • File Attachment control
    Image
  2. Click on the Text Box control and add an expression called StartName.
  3. In the Operators tab under Text, drag the Find function as shown below:
    Image
  4. In the Context tab, drag the File Attachment control and type FileName as shown below:
    Image
  5. Drag the Plus operator from the Operators node in the Operators tab then type 16 as shown below:
    Image
  6. The Preview should resemble the following: Find ( File Attachment, FileName) + 16
  7. Click OK.
  8. Add another expression called EndName.
  9. Follow the steps above to create an expression that will resemble the following in the Preview pane: Find ( File Attachment, field name=’FilePath' ) – 18. (Tip: field name=’FilePath' must be typed in the input field).
  10. Click OK.
  11. Before adding the last expression, ensure that None is applied as the expression to the Text Box control to ensure that no circular reference errors occur when creating the expression.
  12. Add another expression called FileName.
  13. Use the Mid function in the Text node of the Operators tab then drag the File Attachment control into the statement as shown below:
    Image
  14. Now drag the StartName and EndName expressions and add the Minus operator as shown below. The Preview should resemble the following: Mid ( File Attachment, StartName, EndName – StartName )
    Image
  15. Click OK
  16. Apply the FileName expression to the Text Box control
  17. Save and Run the View
  18. Attach a file to the File Attachment control and note how it populates the Text Box control with the name of the file
    Image

 

 

Notes

 

 

The reason why this method is necessary is because the transfer from the Attachment yields a string collection similar to the following:

<collection><object><fields><field name='FileName'><value>Leave Request.docx</value></field><field name='FilePath'><value>C:SourceSFSPSmartFormsWebFiles1714b078-a55b-481d-b89e-22c3f5e38000.png</value></field></fields></object></collection>

String-handling principles are then used 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.

 


0 replies

Be the first to reply!

Reply