NetTalk Central

Author Topic: If possible, how do I display an image from a BLOB?  (Read 5238 times)

DonRidley

  • Don Ridley
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 729
  • donaldridley2011@gmail.com
    • View Profile
    • Email
If possible, how do I display an image from a BLOB?
« on: May 22, 2009, 06:15:43 PM »
I want the user to click on a record and the image associated with that record to be displayed.   Can that be done using NetTalk web server? 

Thanks and thanks to Capesoft for a great product!

Don
"Eliminate the impossible, whatever remains, however unlikely, must be the truth."

NetTalk 12.55
Clarion 11

bshields

  • Sr. Member
  • ****
  • Posts: 392
    • View Profile
    • Inhabit
    • Email
Re: If possible, how do I display an image from a BLOB?
« Reply #1 on: May 23, 2009, 02:35:17 AM »
Hi Don,

I've looked at Bruces' code for BLOBs and it doesn't look finished, so looks like we are on our own.

But i'm sure it is possible. As the image is inside a BLOB, you'll probably not want to copy it from the BLOB and into a temp file as this is going to waste time and require cleanup.

This is what i'd do, it might seem a bit weird, but maybe someone can come up with a better idea :)

After you read this you might much rather store your images on the disk and not in BLOBs :)

1. We need to send the image to the client as if it is a normal image, so i need a syntax nettalk understands to mean "get me a blob but make it look like a image" .

So lets just make that up:

VirtualImage/Prefix_Sysid_Blobname.ImageType

Virtualimage is a folder under your WEB folder that does NOT exist (its imaginary). We do this so we can intercept this call at the WebServerHandler level.

Prefix is your file prefix so we can figure out what file the blob is in.

SysID is your unique field value to locate the correct record. (remember that every request to the web server has no idea what your were up to earlier, so everything needs to be passed)

Blobname is the name of your blob field, in case you have more than one in each file.

ImageType this is either JPG, PNG etc etc, otherwsie you might confuse the browser by not helping it identify what sort of image is arriving.

2. Now with this crazy syntax, we will now have enough info to find the record in question, load it, find the blob, and feed it down the pipe to the browser. Aweful huh, So lets intercept this syntax in WebServerHandler.

In the embed point _SendFile PROCEDURE in Local Objects of WebServerHandler

!the page/file requested wasn't one our our normal pages. So NetTalk will try and find it on the disk and send it, so in our case lets see if its one of our special ones
IF INSTRING('virtualimage/',lower(p_FileName),1,1) ~= 0               
  !Yeap, it starts with our special code virtualimage
 
  This is where you parse the p_FileName variable pulling out all the bits we stuck in the image name, open the file, get the record, determine the file and have the BLOB ready

  SELF.SendMemory(Pre:Blob[0:Pre:Blob{PROP:Size}-1],Pre:Blob{PROP:Size})
  RETURN

.


Now there are lots of little things to go wrong, but i've used a similar technique to redirect images from one server to another to get around Flash security, plus another similar technique to power an email tracking system (using imaginary images).

Hope that give you are few ideas, or discourages you away from BLOBs, or better still Bruce may rescue us both from my hacking.

Regards
Bill Shields

Alberto

  • Hero Member
  • *****
  • Posts: 1872
    • MSN Messenger - alberto-michelis@hotmail.com
    • View Profile
    • ARMi software solutions
    • Email
Re: If possible, how do I display an image from a BLOB?
« Reply #2 on: May 23, 2009, 04:35:26 AM »
Don
I've discuss this with Bruce a long time ago and yo need to have the file on the disk.
The code is prety simple, you only need to use the FILETOBLOB or BLOBTOFILE Clarion functions and it works ok.
You can use a code like this in the WebHandle.Handlefile to store an image in a blob
To retreive the image simply use BLOBTOFILE to a temp file.

  IF P_NAME='PIC:FILE'
    PIC:ID = p_web.GetSessionValue('PIC:ID')
    if Access:CR_PIC.Fetch(pic:xid_pic)
       message(p_web.GetSessionValue('PIC:ID')&' image record not found -  ERROR: ' & ERROR())
    else
 
       PIC:FILE = CLIP(self.site.webFolderPath) & '\' & ReturnValue
       IF FILEtoBLOB(PIC:FILE,PIC:FILE2BLOB)
          MESSAGE(CLIP(PIC:FILE) & ' was not copied -  ERRORCODE: ' & ERRORCODE())
       END
 
       Access:CR_PIC.Update()
     
!       IF BLOBtoFILE(PIC:FILE2BLOB,pic:file)
!          MESSAGE(CLIP(PIC:FILE) & ' was not copied -  ERRORCODE: ' & ERRORCODE())
!       END
 
 
    end
  END
-----------
Regards
Alberto

DonRidley

  • Don Ridley
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 729
  • donaldridley2011@gmail.com
    • View Profile
    • Email
Re: If possible, how do I display an image from a BLOB?
« Reply #3 on: May 23, 2009, 12:31:43 PM »
Thanks Bill and Michelis.   
"Eliminate the impossible, whatever remains, however unlikely, must be the truth."

NetTalk 12.55
Clarion 11

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11245
    • View Profile
Re: If possible, how do I display an image from a BLOB?
« Reply #4 on: May 24, 2009, 09:35:21 PM »
Bill is almost right.
Alberto is incorrect.

Example 40 shows you how to serve any "file" from a blob. It doesn't matter if it's an image, a js file, css or whatever. Indeed you can put your whole Web folder into a single TPS file if you like.

Example 40 even ships with a little utility app for _adding_ the file to the Blob in the first place.

Cheers
Bruce

bshields

  • Sr. Member
  • ****
  • Posts: 392
    • View Profile
    • Inhabit
    • Email
Re: If possible, how do I display an image from a BLOB?
« Reply #5 on: May 25, 2009, 12:45:01 AM »
Thanks Bruce.

Checked out Example 40.

Works good for when there is one "standard" blob file, but more importantly serves as a perfect framework for solving any blob downloading issue.