The Blob data type allows storing images, spreadsheets, videos, audio files, as well as any type of documents, in the database, taking advantage of the different mechanisms of integrity and control provided by the DMBS.
Since 9.0 version, the usability of blobs in the web form has notoriously increased.
Nowadays, there is a way to save blob fields automatically in the database using a web interface (web transaction). There is also an easy way to display a blob field in the web form.
An "upload" control is generated, and optionally, an embedded window in which the content (image, video, etc) is shown.
The file selected with the "upload" control, is loaded to the database automatically.
A blob in the web form, can be visualized automatically as an image, video, audio, document, etc, depending on the content of the blob field. It can be displayed as a link, to allow the user to click on it and display the blob, or it can be shown embedded on the web form.
In order to insert or update blob fields in the database, a file selection dialog appears which enables the end user to browse the file system structure of the client PC (where the Internet browser is running). Then, the process of uploading the file is automatic, he can choose a file that will be uploaded to the server and saved in the database.
The file selection dialog is shown by clicking a "browse" button associated to an edit control in which the selected file is loaded. This is known as an "upload control", and it's shown in the following figure:
And in run-time:
The file selected is uploaded temporarily to a directory of the web server, and then, on the next POST (when the user confirms the transaction) it is saved in the database.
When the blob stores heterogeneous types of files
In some cases, it is necessary to work with heterogeneous types of files, for example, the same field of the database keeps Word documents, photographs in JPG and GIF format, and video files.
As a consequence, in some cases, it may be necessary to capture and save the extension of the files while uploading them, so as you can have this knowledge at the moment of downloading the same files.
The fact of knowing the file extension, helps in many cases in which the Internet Navigators need to know the file format in order to display them, or in cases in which the WebServer prohibits uploading files without extension.
There is a way of capturing automatically, the Name and Extension of the file that ´s being uploaded, besides, this information will be used at the moment of downloading the file.
The main goal is to be capable of running the application in those servers which have maximum level of security and don ?t allow uploading files without extension.
In sum, you have two alternatives if you want to keep heterogeneous files in the same field of the table (docs, jpgs, xmls, etc):
- Define the blob as it is. When the file is uploaded and downloaded, a temporary file without extension will be saved in the file system of the WebServer. At the moment of downloading it, as the file has no extension, it may be necessary to assign a content type (*) to it, in order to give a hint to the navigator, about the format of the file. Some navigators, as IE, don ´t need that hint.
- If you have a WebServer with very strict security policies (like Windows 2003 server using IIS), by default, it doesn ?t allow uploading files without extension. In that case it is better to configure the FileTypeAttribute and FileNameAttribute blob properties in order to capture the extension and name of the blob at the moment of uploading it. The file will be saved temporarily with the corresponding extension. Besides, when downloading (getting) the blob, it will be saved with this extension also, and its original name.
Since now on, we´ll be explaining how to use each of these properties.
Managing different Content Types
Content Types are a means of identifying web pages, images, and multimedia files on the Web and in email. They allow web browsers and email clients to display content in a correct and safe way. In the case of blobs, it may be necessary to set its Content Type, specially for certain kind of files, or for certain Internet navigators. Commonly used content types include text/html, image/jpeg, model/vrml, video/quicktime, etc.
It´s important to note that GeneXus automatically generates the corresponding Content Type of each blob, depending on the extension it has. Anyway, there are ways to change this Content Type, if the user wants to.
There are two ways to do this in GeneXus:
1. ContentType Property (design and runtime control property)
2. FileType Property (design and runtime property)
Retrieving the name of the file
You can query the name of the file by using the FileName Property (runtime property).
Display Control Properties
Determines Width/ height of the blob control in the form.
It indicates the way the blob content is going to be displayed in the form. Is available in design model and at runtime. The possible values are:
- 0: Inline - The blob is displayed in the form, above the upload control.
- 1: Link - An image is displayed with a link to the Blob.
When the display property is set to "Link", by this property you can choose to show the blob content in a separate window.
For example: blobatt.linktarget = '_blank'
When the display property is set to "Inline", and the blob content can't be managed by the internet browser without providing to it additional information, the Parameters property has to be used in order to provide the needed information to the browser.
For example, if you want to display a video in mpg format, in the IE (Internet Explorer), you have to set:
(This example runs in Windows Media Player)
blobatt.parameters = '<param name="Filename" value="' + pathtourl( blobatt) + '">' +
'<param name="AutoStart" value="True">' +
'<param name="ShowControls" value="True">' +
'<param name="ShowStatusBar" value="True">' +
'<param name="ShowDisplay" value="False">' +
'<param name="AutoRewind" value="True">'
- The internet browsers, need to know which application to use to display certain type of files. That is why the ContentType property exists. For example in the case of IE, when the content is .mpg, the ContentType shouldn't be video/mpg (as it would be by default), but application/x-mplayer2. In this special cases, the ContentType makes reference to the application which should show the content instead of the real content type of the file.
Blob Local Storage Path
This model property is used to set the web server path where files are temporarily saved when retrieved from the database. That is to say, when you Get data, a temporary file is saved in the Blob Local Storage Path. The saved file's extension depends on the value of the Blob property called File Type.
The Blob Local Storage Path must be accessible from the virtual directory. Ie, if the virtual directory is C:\resin-2.0.2\webapps\test an acceptable value for the property could be: C:\resin-2.0.2\webapps\test\resources\rtrepository.
Temporary Media Directory
This model property is used to set the web server path where files are temporarily saved when loaded in the database. That is to say, when a Blob is added or updated, it is temporarily saved in this directory.
The Temporary Media Directory must be accessible from the virtual directory.
- For security reasons, there are two different properties. However, GeneXus users may set them to point to the same physical directory. The Blob Local Storage Path will be always accesible to the user.
- In NET generator, by default, the Temp Media directory and Blob Local Storage path properties are considered to be the folder WEB under the model directory.
pathtourl( att) /character
It returns the URL under which the blob content is accessible in the web server (see Blob Local Storage in this document).
It should be used in case that the user needs to know this URL.
For example, it should be used when loading an image using the loadbitmap function:
&url = pathtourl(BlobAttribute)
&Logo = loadBitmap(&url)
Consider a table wich stores the employees information of a company, such as name, address, photo and personal information of each person working in the company. Now it's easier to save an employee's photo in the database, update it, download it to the client pc and view it in a web browser. To do so, you only need a web transaction with the blob field in the web form. An upload control is automatically generated to save the photo in the database. Likewise, when querying an employee's data you can view his/her photo. It is automatically loaded in the blob control without any additional progamming.
Besides, if heterogeneous files are saved in a table field, the file extension of these files can be managed by the FileTypeAttribute property.
To test this example, consolidate the xpz in a GeneXus 9.0 KB and set the following model properties:
1. Blob Local Storage Path: It must be below the virtual directory, and indicates where the files are saved when retrieved from the database.
2. Temp Media Directory: It must be below the virtual directory, and indicates where the files are saved when added to the database.
For example, when working with the .Net generator, create a "Media" folder under the model web directory and set both properties with the value: Media
You can download the sample from here:
I. In general, Internet Explorer doesn't respect the Content Type returned by web servers and uses a variety of strategies to automatically detect the content type of files (for more information see the following Microsoft Documentation: http://support.microsoft.com/default.aspx?kbid=325241).
This means that there are different behaviors among browsers. For example, when a blob field is displayed as a download (link), and a Content Type is specified, IE determines how to display the blob in the form by using the above strategies. Therefore, the Content Type is only considered as a hint for the browser.
This doesn't happen with Mozilla, which always considers the specified Content Type. Therefore, it's advisable that you read the corresponding documentation so as to know beforehand the application's expected behavior.
Our tests have thrown the following results:
1. When the blob is displayed as link, the Content Type is ignored by IE. It has its own rules to identify the content type and ignores the one specified by the user. The same happens with Netscape. As a consequence, the file will be correctly displayed in the HTML only if the file type can be identified by the browser. To solve this, give the file an extension through the FileType property when saving it temporarily. This problem doesn't exist in Mozilla or FireFox
2. If you set the FileType property, the blob is temporarily saved in the server with an extension. In this case, the web server decides how to display the content through MIME Types, so the specified Content Type is ignored. Blobs that are Word documents, for example, are correctly viewed only if they are displayed as links, and they have a an associated extension or "FileType" (the same happens with Excel worksheets).
3. In some cases, when the blob display is Inline and has an associated extension (File Type), IE can't display the content, while Netscape and Mozilla try to download it (for example, it happens with .doc, .xls, .pdf).
4. If the display is Inline and you work with File Type or Content Type, IE, Netscape and Mozilla all consider the specified Content Type, except for Word documents and other exceptions. Some MIME types, like graphics, can be displayed inside the browser. Others, such as word processing documents, require an external helper application to be displayed.
The following table shows some Content Types which are assigned automatically, given a File Type (an associated extension).
II. Changing the Temporary Media Directory and Blob Local Storage Path at Runtime
You may change the Temporary Media Directory and the Blob Local Storage Path at runtime for the Java generator by changing the client.cfg. For example:
TMPMEDIA_DIR=C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\YIBLOB\TempMedia
CS_BLOB_PATH=C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\YIBLOB\media
In the .NET generator you need to change the web.config.
In Visual Basic, the information is hard-coded so you can't change it at runtime.
Comments & Collaboration
Q:How did it work in previous versions (GX 8.0) ?
A:Previously, a blob field in the web form was generated as an edit control, which content was the path to a temporary file in the web server. That is to say, each time you read a blob field the file content of this blob was saved temporarily in a certain directory of the web server. This temporary path was shown as the blob field in previous versions.
In order to visualize the blob as it is, it used to be necessary to load it in a variable, for example, to show an image it should be necessary to define a bitmap variable and to assign the loadbitmap of the blob field to it.
Objects: Transactions, Web Panels
Languages: .NET, Java.