Storing and retrieving image files in a database

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Re: Storing and retrieving image files in a database

    hi JerkyRobot I can see no one answered your question. here is the answer.
    a few things you will have to do before this will work. 1) you need to tweak
    your DSN in the Advanced settings. make sure the CLOB (enable long text
    retrieval) is checked. 2) make sure the table column you are storing the data
    in is longtext or comparable. keep in mind this is Linux and MySQL. <!---
    first we read the binary file ---> <CFFILE ACTION='ReadBinary'
    FILE='/your_path/your_image.jpg' VARIABLE='aBinaryObj'> <CFIF
    isBinary(aBinaryObj)> <CFSET base64 = toBase64(aBinaryObj)> <CFELSE>
    <CFABORT showerror='The file does not appear to be a binary file'> </CFIF>
    <!--- if everything is cool, we insert the data ---> <CFQUERY
    datasource='your_DSN'> INSERT INTO your_table (your_field) VALUES
    (<CFQUERYPARAM VALUE='#base64#' CFSQLType='CF_SQL_CLOB'>) </CFQUERY> <!---
    now to get the data back, just query the DB ---> <!--- now we use cffile agian
    to convert it back to an image ---> <CFFILE action='WRITE'
    file='/your_path/your_image_name.jpg' output='#toBinary(getImg.attachment)#'
    addnewline='No'> <!--- there you go. this will work for any binary file
    (.exe, .zip etc.) --->

    Duke Snyder Guest

  2. Similar Questions and Discussions

    1. Database Storing
      I have created and asp form and connected to a sql database on dw. What is the next step for getting the submit button to store the form info in the...
    2. MySQL Database not retrieving the full database
      Hi, I am using MySQL and PHP for my repository. It has 500+ records. till now it was displayiing all records in the database, but since from one...
    3. Storing and Retrieving Info in Access
      I'm not sure if this is even the best place to ask this question, but here goes: my site is hosted by a third party on a Win2K platform and there...
    4. Storing and retrieving arrays using Berkeley DB (Sleepycat)
      Hi all, I have experience with PHP, but I am totally newbie on Perl. I need to use Berkeley DB to store/retrieve some records. The samples I...
    5. Storing/Retrieving/Executing Php
      I want to creating an application where all my php(with html) is stored in the database. So my index.php would find the appropriate the php file...
  3. #2

    Default Re: Storing and retrieving image files in a database

    another way to do it, assuming it's been stored as a BLOB (or whatever your
    database calls binary info) is to do a simple select query on it, then output
    it to the browser like so: <cfcontent type='image/jpg'>
    <cfoutput>#toString(your_image_field_here))#</cfoutput> CFCONTENT and CFHEADER
    have lots of cool uses.

    Superflyc Guest

  4. #3

    Default Re: Storing and retrieving image files in a database

    Hi Duke Snyder, I copied your code and changed CF_SQL_CLOB to CF_SQL_BLOB and
    tried to run it but i got a following error 'rror casting an object of type
    to an incompatible type. This usually indicates a programming error in Java,
    although it could also mean you have tried to use a foreign object in a
    different way than it was designed.' I am using CFMX 7 and oracle 9i is the
    db. I also tried to run the code using CF_SQL_CLOB but that gave me the
    following error 'Macromedia][Oracle JDBC Driver]Value can not be converted to
    requested type.' any ideas? thank you

    shahzad Guest

  5. #4

    Default Re: Storing and retrieving image files in a database

    nevermind, someone helped me to fix the problem. For reference for the others,
    i shouldn't be converting the image to base64 because my datatype for the
    column in the db is BLOB. if it was CLOB, then i would convert it to base64

    shahzad Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139