K2

How to upload multiple files with K2 Smartforms

Upload multiple files with k2 smartforms

As a K2 developer you are often required to provide users with the ability to upload and store multiple files, and make these files available to other users. This is very easily achievable with K2 Smartforms. In this post we will do a quick run-through of how to accomplish this.

When K2 stores a file it makes use of an XML data structure to store the file name, as well as a base64 encrypted string which contains the actual file content. The XML structure looks as follows:

1 - K2 SmartForm Xml File Structire

1 – K2 SmartForm Xml File Structire

The first step is to create a table in which we will store the files. Our table will simple consist of a database-generated id as primary key and an XML field to store the file, as indicated in the screenshot below.

Upload multiple files with k2 smartforms

2 – SQL Table to store attachment

Next we need to create a smart object which we will use to access this table. The simplest and quickest way to achieve this is by using the SmartObjects – Services – Tester. Simple right click on the table and select ‘Create Smart Object’, as indicated in the screen shot below.

3 - Publish SmartObject

3 – Publish SmartObject

In the wizard screen that follows simply give the Smart Object an appropriate name, select a Category , and publish it (red arrow). Note that the XML field is of type ‘Memo’ in the Smart Object (green arrow).

4 - Publish SmartObject

4 – Publish SmartObject

Now that the Smart Object has been published we need to change the data property from the current memo field type to a file field type. Select your SmartObject in the designer, and press edit.

5 - Edit SmartObject

5 – Edit SmartObject

Now select the Data property and once again press edit.

6 - Edit SmartObject Properties

6 – Edit SmartObject Properties

In the Type drop-down box change the type to File, press OK and Finish.

7 - Edit SmartObject Properties

7 – Edit SmartObject Properties

Next we need to build a view in K2 Smart Forms. In the first screen of the view designer give your view a name, select List View as the view type, select your newly created Smart Object as the Data Source and the List method of the Smart Object as the List method, then press next.

8 - K2 SmartForm for Uploading attachments

8 – K2 SmartForm for Uploading attachments

In the Layout screen simply select the ‘Create Labels and Controls’ option.

9 - K2 SmartForm for Uploading attachments - Views

9 – K2 SmartForm for Uploading attachments – Views

In the following screen select to include the Data field and Enable list editing. Also select the options to allow a user to add, edit and remove rows, and press OK.

10 - K2 SmartForm for Uploading attachments - Settings

10 – K2 SmartForm for Uploading attachments – Settings

K2 will generate a default layout for you. Now you are good to go, it is as simple as that. Running the view will present you with the following screen. To upload a file click on the Add option.

11 - K2 SmartForm for Uploading attachments - Add Attachment

11 – K2 SmartForm for Uploading attachments – Add Attachment

Now select the ‘Click here to attach file’ option, attach your file and press save.

1 - K2 SmartForm Xml File Structire

1 – K2 SmartForm Xml File Structire

To download the file simply click on the file itself (indicated with a blue arrow in the screenshot above).

For more information about K2 SmartForms, click here

Upload multiple files with k2 smartforms

You may also like

Leave a comment

PODCAST  |  Why you need to establish a K2 Center of Excellence today

Interview with Arno van Rooyen, CEO, Velocity