Ever wanted to save an image to a database with ColdFusion? I see this question asked quite often. I don't have a need to do this but I mocked up a quick demo or reading and writing binary data to and from a mySQL database.
The code is very simple. I read the binary data from a image. Write it to a database, read it back out and write it to the browser. I also rotate the image to show the result from the database acts just like any other image data.
You might note I made use of 2 great new features of ColdFusion 9.
1. Data source specific to the application. I no longer need to specify a datasource in my cfquery tags as I have this setup in Application.cfc.
2. On insert ColdFusion can return the autoincrement id value of the record just inserted. Eg in SQL server select @@identity does the same thing. This was available in CF8 but it was lacking and was different depending on the database type.
In ColdFusion 9 the old values still work ( so code wont break ) but we also can now use queryResult.generatedKey.
I always try include a working demo: http://cf9.kukiel.net/demos/imageinsert/
Here is the CF Code:
Here is a sample to create the mySQL table:
Also note as this gets alot of people that by default CFadmin only allows 64k to be passed from CF to the database. You will probably need to increase this value. For this example I just added 2 extra 0's ( 6.4meg ). If you don't half the image will be inserted and you will be confused as to what the image ( or other binary files ) are corrupted.
Subscribe to:
Post Comments (Atom)
Categories-
- Actionscript (5)
- Adobe (8)
- AIR (3)
- Apple (2)
- BlackBerry (1)
- CFML (59)
- ColdFusion (84)
- ColdFusion 8 (12)
- ColdFusion 9 (28)
- Database (2)
- Flash (11)
- Flash 10 (6)
- FlashBuilder (4)
- Flex (31)
- Internet Explorer (1)
- iPhone (4)
- Java (8)
- jQuery (1)
- Linux (8)
- Microsoft (5)
- Open Blue Dragon (10)
- Open Source (11)
- Operating Systems (3)
- PHP (1)
- Railo (24)
- SQL (3)
- SVN (2)
- Twitter (2)
- VPS (1)
- Windows (5)

Blog Archive
-
►
2010
(38)
-
►
August
(9)
- ColdFusion and cfquerynew and date times - timesta...
- ColdFusion Lists, SQL IN and cfqueryparam
- iPhone and Flash - Sucess
- ColdFusion jrun and the JVM
- ColdFusion and CFReport
- jQuery or EXTjs
- iPhone 3Gs and Flash player
- Easy way to embed ColdFusion code into your blog
- Flex and the MATE framework in Melbourne
-
►
August
(9)
-
▼
2009
(89)
-
▼
December
(17)
- ColdFusion 9 and the local scope continued
- Is this an issue with the local scope in ColdFusio...
- Are you using ColdFusion 9's ORM?
- Want more performance from a default Railo install...
- WizzyWig the comic and snow
- Linux Start and Stop scripts for Ventrilo - Ubuntu...
- ColdFusion Builder Beta 3 - Any one seen this?
- Streaming AMF channel with ColdFusion 9 and BlazeD...
- Forwarding or masking your domain with GoDaddy
- Cheap ColdFusion hosting
- Should I get an iPhone?
- Full Screen Flex
- Googe Fade Demo
- Google now providing public DNS
- Cumulative Hot Fix 4 for ColdFusion 8.0.1
- Cheap bundled Apple Mac software
- Reading and writing binary to a mySQL database wit...
-
▼
December
(17)
Reccomended:
- TweetTrail
- Load Cell
- MyTouch Cases
- View Prom Hairstyles
- Cell Phone Accessories
- business operating agreement
- learn how to recycle electronics



9 comments:
What are the advantages (and disadvantages) to save the image in the database?
One advantage might be that you can detach a database and move it to a new host without needing to copy the image files.
A disadvantage is that there is overhead to and from the database.
I'm sure there are many more pro's and con's and I'm interested to hear what others have to say.
Wouldn't it save server space? - if you have a site that has users submit images - wouldn't writing them to the database be more efficient as far as space?
Well it would still use space on the database server. Each "blob" is still the same size as the file its just stored in the database rather then the file system as a file.
Pros:
-You can secure the "files" on your app much easier.
-if you have a multi-app server environment, you can share images/files/etc across the applications without duplicating files.
cons:
-i think there is a little bloat storing as blobs (not 1:1 file to blob IIRC)
-can't take advantage of webserver auto-caching the content
-more load on your app server versus the file server
Thats all I got right off the bat.
In Aaron's list of cons, you could add more load on the database server too, couldn't you?
Aren't some of the new "No SQL" databases good for storing documents like images too? Just curious.
@Chris
Yes without a doubt this would cause an increase on load for the DB server. Servering a static image via apache or IIS I imagine the load its very tiny compared to retrieving it from a database.
Nice post Paul.
One huge adavantage of this is when running in a cluster with replicated databases, everything is replicated all text and binary content.
It is a big slower, but you can also cache the binary data on disk and only update the database when it changes.
One thing that might be worth your testing relates to the 64k buffer behavior. In past versions of CF inserting to SQL Server, I found that the full data stream was written to the database regardless of the buffer setting. Retrieval, however, was truncated if the data exceeded 64k without adjusting the buffer. No lost data, just inaccessible if it exceeded the buffer size.
If it's a busy site, you might also consider having a seperate "blobDSN" for use with the increased buffer setting. There is a performance impact associated with the buffer setting that you may not want to impose on every database transaction.
Post a Comment