Murray Collingwood wrote:
Hello again

I'm on the hunt for some sample code that will show me how to use my SQL Blob fields to store images or documents.

Here's something on the Wiki to get you started:

http://wiki.apache.org/struts/StrutsFileDownload

The sample app posted there includes serving an image from a database.

For a while I entertained the idea of keeping the images and documents in a sub-
directory and simply referencing the file names but then I realised I was going to get into trouble with synchronising the SQL data with the files on disk. For example, what if somebody manually deleted one of the files, suddenly I need a bunch of conditions to handle these inconsistencies.

Not sure what you mean here... it sounds like you need to store some info in the database to give some details on what's on the file system, but I'm not sure what you see as the problem here.

From my reading of Blobs in the SQL manual (MySQL 4.1) I can simply treat them like
objects and populate them into forms etc without any problem. So how do I get the image to appear on the html page? Is it as simple as:
<html:img name="attributeName" property="myblob" /> ?

Not really... while I wouldn't be surprised if there was some provision for getting a BLOB field to an Object, that's not what is typically done.

You need to understand the basic HTTP model here... let's say you do some processing no the server and ultimately wind up in a JSP. The JSP renders HTML, which is returned to the browser. Let's say the following appears in the HTML:

<img src="myImage.gif">

Simple enough... what happens is the HTML is interpreted by the browser, and a new request is made to the server for that image. It is at this point you need to do something (maybe)... if the file is simply on the file system, so long as the path is correct, the image will be returned.

What if the image is in the database though? You will need to have a servlet (or Action) that retrieves the BLOB and returns it to the browser. This is typically done by gettting a stream on the bytes of the BLOB and writing them to the response.

How do I upload an image and capture it into a form?

Uploading is done using an <upload> element, presuming you are uploading via HTTP. The important thing to note here is that Struts isn't really involved... the BLOB will never be in the ActionForm for instance, either when uploaded or downloaded. There are numerous examples of accepting uploads in Struts, and any of these can be the basis for what you are doing. You will just need to write the image to the database at that point.

Regarding the SQL management of blobs, I'm assuming that it is better to create a separate table for these with two columns, ID (int auto_increment) and IMAGE (blob), and then to use the ID to reference the image on the other tables where it is linked. Does this sound right to you?

Probably right... couldn't say for sure without knowing your DB schema a bit.

--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to