10.3. File-system approach

DbForms also supports the storage of files on the file-system. This is completely transparent to the JSP (view) developer! For uploading and retrieving file-system-stored objects, the same tags and attributes are used as for uploading and retrieving BLOBs.

The difference lies only in the definition of the model; instead of fieldType blob, we define a type called diskblob. I invented this term because I think it reflects what it represents a binary large object stored to disk (instead of to the database)

10.3.1. Defining DISKBLOBs in the model definition

As the following example and table show/explain, there are additional attributes available for defining a diskblob-field:

           <table name="pets">
               <field name="pet_id" fieldType="int" 
                     isKey="true" autoInc="true" />
               <field name="name" fieldType ="char" />
               <field name="portrait_pic" 
                      fieldType ="diskblob"
                      encoding="true" />
               <field name="story" 
                      fieldType ="diskblob" 
                      directory="x:\stories" />

Table 10.1. define a table capable of Blobs (depends on SQL dialect implemented by RDBM vendor)

Directory [this attribute is required]Holds the directory uploaded files should be stored to. This directory must exist and b) must be writeable by the web-server
Encoding [this attribute is optional]Holds either “true” or “false”. If "true", then files will be renamed to ensure that every file is unique and no file overwrites another. If this attribute is not specified, the default "false" is applied (keeping original names and overwriting equal-named files)

With the definition shown in the example preceeding the table, we can expect the following behavior:

If a portrait_pic (lets say dog.jpg) is uploaded, it will be stored in directory c:\upload\pets under a unique name like 34834893483_748734.jpg)

If a story (lets say dog.pdf) is uploaded, it will be stored in directory x:\stories (which may be a virtual directory pointing to another computer-box) under its original name.

10.3.2. What goes on behind the scenes

You might ask: How does DbForms remember where it stores the files? The answer is simple: the references to the actual physical files are stored in the specified DISKBLOB field, which turns out to be an ordinary alphanumerical (CHAR, VARCHAR, or similar) field.

Following our example, the pets table would now contain a row such as the one displayed below:

Figure 10.1. Physical representation of our example

Physical representation of our example

Both files are stored in their respective directories (c:\upload\pets x:\stories). The directory names are not included in the references for two reasons (please email me any arguments to the contrary):

It is not necessary because this information is managed by the XML-configuration

The value may be retrieved using a simply db:label or db:textField tag. It is not a good idea to expose server internals (directory structure) to the clients.