This Knowledge Base article describes how to extract the Filename from an uploaded File or Image.
- Form Designers
- Expression Builder
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:
- Create a View that contains the following:
- Text Box control
- File Attachment control
- Click on the Text Box control and add an expression called StartName.
- In the Operators tab under Text, drag the Find function as shown below:
- In the Context tab, drag the File Attachment control and type FileName as shown below:
- Drag the Plus operator from the Operators node in the Operators tab then type 16 as shown below:
- The Preview should resemble the following: Find ( File Attachment, FileName) + 16
- Click OK.
- Add another expression called EndName.
- 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).
- Click OK.
- 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.
- Add another expression called FileName.
- Use the Mid function in the Text node of the Operators tab then drag the File Attachment control into the statement as shown below:
- 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 )
- Click OK
- Apply the FileName expression to the Text Box control
- Save and Run the View
- Attach a file to the File Attachment control and note how it populates the Text Box control with the name of the file
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:\Source\SF\SP\SmartForms\Web\Files\1714b078-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.