Reading and writing binary to a mySQL database with ColdFusion
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://railo.kukiel.net/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.