1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Save Image FILE to database, retrieve and display

Discussion in 'Programming' started by datropics, Aug 23, 2007.

  1. #1
    Hey guys,

    I thought this was easy and as surprised that I couldn't figure it out. Have any of you done this? Basically what I want to do is:

    1. Allow the user to submit an image
    2. Save the submitted image to the database MS SQL 2000
    3. Retrieve the Image
    4. Display the Image

    Thanks in advance for the help.

    daTropics
     
    datropics, Aug 23, 2007 IP
  2. advantage

    advantage Well-Known Member

    Messages:
    1,338
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Why do you want to store the image to the database? It's doable - but not recommended because the image will then have to be transferred twice (from db server to web server then from web server to client) doubling the bandwidth used on your web server.

    Not to mention being detremental to the caching benefits of coldfusion and mysql, having a large ram footprint on both the sql server and your http server etc etc.
     
    advantage, Aug 23, 2007 IP
  3. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Yeah I know - it's a requirement, can you help me out?
     
    datropics, Aug 23, 2007 IP
  4. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    IIRC
    1. Use the "image" data type for the storing the images in the table.
    2. Insert the image using cfqueryparam and the "cf_sql_blob" type
    3. Retrieve the image in a query and use cfcontent with the "variable" attribute to display the image
     
    cfStarlight, Aug 23, 2007 IP
  5. advantage

    advantage Well-Known Member

    Messages:
    1,338
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Make sure "enable binary large object retrieval" is enabled for your datasource in coldfusion administrator.

    Read the binary from the file field
    <cffile action="readbinary" file="#form.filefieldname#" variable="bin_filedata">

    then

    <CFQUERY ....>
    INSERT INTO tablename FIELDS (........, binarydata, .......) VALUES (........., <cfqueryparam value "#bin_filedata#" cfsqltype="cf_sql_blob" />,.....)

    Hope that helps
     
    advantage, Aug 23, 2007 IP
  6. advantage

    advantage Well-Known Member

    Messages:
    1,338
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    140
    #6
    oh yeah for the submit part
    <input type="file" name="filefieldname">

    for the retrieval / display part
    You have different options - but I prefer to create a file that serves up the image because you can then also check for hot links or put other security measures

    inside show_image.cfm
    <CFPARAM name="image_id" default="">
    <!--- Get image --->
    <cfquery name="grab_image">
    SELECT imagefieldname
    FROM tablename
    WHERE whatever <cfquery>
    (OR it would be better to use a stored procedure here)

    <!--- Set MIME type to GIF or JPEG or ... --->
    <cfcontent variable="grab_image.imageData" ... >


    then inside your display page <img src="show_image.cfm?image_id=_theid_">


    there are other ways if you want to save the extra query for the blob
     
    advantage, Aug 23, 2007 IP
  7. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #7
    Why not let your users upload the image to a folder in your root directory and store the path to the file in the database. Then query the database to retrieve the path and display the image using an <img> tag.

    You can use <cffile> to upload the image.
     
    unitedlocalbands, Aug 23, 2007 IP
  8. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Thanks guys, this is great!

    United - yeah I know what you're saying but the client has this as a requirement - you know how it is,

    Thanks again
     
    datropics, Aug 23, 2007 IP
  9. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Advantage,

    did you try this code yourself?
    does it work?
    I'm getting an error, which version of CF are you using?
    The client has 4.5, would that be an issue? (don't laugh)

    thanks!

    daTropics
     
    datropics, Aug 23, 2007 IP
  10. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #10
    Got them go to CF7 - working - thanks!
     
    datropics, Aug 23, 2007 IP
  11. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Nice to know that not everyone is using the latest and greatest. A coworker recently switched to eclipse, but before that they were using CF Studio 4 ;-)
     
    cfStarlight, Aug 24, 2007 IP
  12. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #12
    Don't know why though - because they are running around trying to do the stuff that the later versions do with ease - can't wait to sink my teeth into CF 8!!!!!!
     
    datropics, Aug 24, 2007 IP
  13. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    VERY true. But I admit I get a kick out of hearing CF4.5 is still being used ;-)

    I can't wait to try CF8 either. CFPDF, CFTHREAD,CFIMAGE, debugging, server monitoring, queries that return identity values - oh boy! :)
     
    cfStarlight, Aug 24, 2007 IP
  14. kmria

    kmria Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    This sounds the best idea to me. So how we do this in the img tag?
     
    kmria, Jan 31, 2011 IP
  15. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #15
    krmria,

    Best to start your own thread rather than resurrecting one that's 4 years old.
     
    cfStarlight, Feb 6, 2011 IP
  16. prptl709

    prptl709 Guest

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    I have one question about storing image in database is that the image field in database has type varchar or image.please help me.
     
    prptl709, Feb 21, 2011 IP
  17. prptl709

    prptl709 Guest

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    If I given the image as varchar in database then it will work or not?
     
    prptl709, Feb 25, 2011 IP
  18. kmria

    kmria Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Why? This thread is still live on google search list so 4 years old or not makes no difference
     
    kmria, Mar 6, 2011 IP
  19. Paul_K

    Paul_K Greenhorn

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #19
    Paul_K, Mar 16, 2011 IP
  20. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #20
    if course it makes a difference ... if you actually want a timely response.

    - resurrecting old threads is what spammers do. I usually ignore them completely
    - old threads usually have tons of responses already. most people don't have time to read a slew of posts just figure out if they can answer the new question. ie When you're getting something for nothing (free advice) don't insult those donating their time by making them work even harder. Most will decide it's not worth the effort ;)
    - .. and after 4 years most of the people you're asking probably aren't even following anymore anyway (notice the delays...).

    but if you want wait for answers or wade through a ton of unrelated comments to get to one ... go for it! ;)
     
    cfStarlight, Jun 16, 2011 IP