A rather common task in web development is to display a list of files on a site and making it accessible in accordance with access permissions. If the list is large, then searches or filters are needed, and a simple list of HTML links won’t do. I needed to create a file archive in .NET Forge CMS and this two-part article will tell about how to make maximum use of the standard means available in the product and quickly create a file archive.
For this, use the Community edition of .NET Forge CMS, which can be downloaded here. In this article I will not cover the details of certain basic issues such as creating a page, infoblock, working in the Control Panel of the product, etc. Before reading farther, I recommend that you have working familiarity with the product.
Task 1. Create a file storage.
To create a list containing any type of data in .NET Forge CMS, information blocks (infoblocks) are used. These are universally configurable lists which can be managed in either the front end or the control panel. Infoblocks contain ‘elements’ (the individual records or elements) and ‘sections’, which act like folders, containing subsequent elements and sections. At the database level, each infoblock is a separate table, and at the level of .NET code, each is a class of ORM BXIBlock.
So, we start off with our infoblock.
Here you can find how to add an information block. According to the instructions, we create a new information block type: ‘My Site’, and within this type, we create the information block Files.
How are we going to save files? Logically, elements in this infoblock must contain files. We open the newly-created infoblock and click ‘Add element’ in the instrument panel. At first glance, it is not clear where on the page we can save a given file. And indeed, right now there is no such place J. But that is easily fixed.
Here is the page with the list of elements of the new infoblock. We open the tab properties using the button on the instrument panel.
All infoblocks support user-made fields (properties) for elements and sections. We can easily add our own field to an element or section, which will hold the information that we need to store. Fields can contain text, counters, whole numbers, etc. It is also possible to create a field of the File type. Code is a key-field, which the system subsequently uses to identify the existence of the given property (field) in the infoblock and then the value of this field. For every type of user-created property there is a corresponding list of settings which can be changed by pressing the ‘Edit’ button, highlighted in red in the picture. We click this button and view several settings of the File field. In the General tab, we click the ‘required’ setting for our field. Now every new element of the infoblock will have to have a file uploaded into it before it can be saved.
Then let’s open the ‘Advanced’ tab.
Here there are two settings of interest – the maximum file size and the allowed file extensions.
In this example, I’ve put the file size at a maximm of 10 MB. After entering the settings, we click Save and we must also click Save in the infoblock settings form, or else the changes will not be saved.
Now we have a list of files which is for the time being empty. We can add elements to the list right now only in the Control Panel, so we will create an interface in the front end.
Task 2. Uploading file to the server.
To start working, we need a page from which files will be uploaded. I have installed an ‘empty’ installation of .NET Forge CMS – without a site solution, although, of course, there is nothing to stop this exact process from being executed in any of the site solutions. We create a new page on the site, and name it File Upload.
To upload a file to the server, we use the iblock.element.webform component, which is a form for adding elements to infoblocks. To add this component to the page, click on Edit page in the front-end administrative panel and the visual editor will appear.
In the top right corner of the list of available components we open the Add Element section and the ‘Add elements to infoblock’ Form. Drag and drop this component into the page field – the left side of the visual editor. When the component is added, click on it and in the lower panel of the visual editor, the components settings will appear – and they now need to be configured. We enter the type of infoblock – My Site, and the infoblock itself – Files. This component will now add elements to this infoblock. Below there are field settings for filling out the form. We add the File field which we added to the infoblock. Scrolling down the settings list, we change the setting for the size of uploaded file (we entered this setting in the properties of our File field, so repeating it here is not necessary). One another important setting is a list of user groups, which will be able to upload files using this component. Let’s allow this operation only to the Administrators group.
Save the component and it appears on the page:
As we see, the field File is marked with a star indicating that the field is required. If we try to add an element using this form without uploading a file, this form will show an error message.
We have just created a form to upload files, and it should work. Let’s configure our system a little bit. By default, when we use .NET Forge CMS for file uploading, it will store uploaded file in /upload/ folder in site root, saving every file in a unique subdirectory to avoid possible coincidence of file names. Also, the file is given a unique name instead of it’s original name. But in our archive we don’t want to upload a file text.txt to the server and let user download this file then as 153ee836a85a8c5b5bcd2bc517617988.txt. So, we should change this behavior, and we can easily do that. Let’s open the system control panel, and go to Settings -> Module Settings –> Kernel. There we turn on the “Preserve original file names” setting and click “Save”. That’s it!
In this article we have created a file storage and simple interface to upload file to our archive. What we have to do next is to display a list of files from this archive on public page and give appropriate permissions to user groups to download these files. I will write about this part of the task in the next post that will come out soon.