I have a general question about how AJAX handles the fetching of images
from a database.
If you are storing images in a database, a standard way of doing it is
to use a BLOB. The traditional web-app way of doing things is to build
a static page on the server using something like php, and then transmit
it back to the client's browser. Recreating an image from a BLOB with
php is a snap:
header('application/JPEG')
echo $imgdata
How do you acheive the same thing with AJAX? I don't want to have my
server creating and destroying images all the time, just so they can be
downloaded by the client browser.
Having done some research, there does not seem to be a JavaScript way
of reinterpreting the BLOB data.
Is the only workaround to simply not store the images as BLOBs in the
first place, and just provide a URL/path?
Thanks in advance.
If you want to change images without reloading your page all you need
to do with AJAX is get back an SRC of the new image you wan,t then
update the SRC of the old image. In some cases (like when you have only
a small number of images to cycle through) you won't even need AJAX.
Your SRC should be something like request_image.php?img_id=4 whose sole
purpose is to get the image data as you have above :
header('application/JPEG')
echo $imgdata
Alternatively you could show the image in an iframe, just give the
iframe the same request_image.php URL
You'll have to think of some ingenious way or requesting the image.
Maybe posting from an iframe?
When SVG support is better across browsers, then AJAX will be perfect
for the situation you describe, provided you want to use that format of
course.
1. Using a small php script in an iframe to display the image. I'm not
sure how you could do that with GWT though - is there a way to make a
widget populate itself with external content?
2. Use the server code which is extracting the blob to turn it into an
actual file on the server, and pass back the URL to it.
All my server code is written in Java, and throwing php into the mix is
pretty messy. On the other hand, the second option needs some fairly
clever maintenance of the created files.
Completely wrong header... You need to re-read HTTP spec.
What I do is I store the location the image is on the filesystem, then
I stream the file contents to the browser.
I quite agree with you. this approach makes sense. also means you can
link static html to that image.
Downside is that other people can link to your image and steal your
bandwidth.
Dean
http://www.oracle.com/technology/products/intermedia/htdocs/why_images_in_database.html
In any case, my database already exists and has images embedded into
it.
Thanks to Dmitry for pointing out my php error. I should have added a
"or something like that" clause after it.
http://www.phpriot.com/d/articles/database/images-in-mysql/index.html
I don't think it refers to the specific storage engines though.
http://dean.edwards.name/weblog/2005/06/base64-sexy/
It doesn't work in Internet Explorer though, and I believe the
workaround involves passing all the image data back to the server, and
then back again as the image. It'd be easier just to call a separate
php script which fetches the image from the database on it's own.
Still, once IE7 is released this may become a more standardized way of
passing image/binary data around.