Storing and loading images

jdpjamesp

ProgressTalk.com Moderator
Staff member
Hi all, having a slight problem with images stored on the db. We're having to store them in chunks so that we can run on an SQL back end :(

The code to store the image:
Code:
    /* convert the file into a memptr */
    FILE-INFO:FILE-NAME = lvFile.
    SET-SIZE(memptrCDoc) = FILE-INFO:FILE-SIZE.
    INPUT FROM VALUE(lvFile) BINARY NO-CONVERT.
    IMPORT memptrCDoc.
    INPUT CLOSE.

    /* Get the filename stripped of the path */
    ASSIGN lvPos = R-INDEX(lvFile,"/").
    IF lvPos EQ 0 OR lvPos EQ ? THEN
      ASSIGN lvPos = R-INDEX(lvFile,"\").
    IF lvPos GT 0 THEN
      ASSIGN lvReference = SUBSTRING(lvFile, lvPos + 1).

    /* Get the size of the document */
    ASSIGN lvDocumentSize = GET-SIZE(memptrCDoc).

    DO lvRecordCount = 1 TO lvDocumentSize BY {&DOCUMENT_PARTITION_SIZE}:

      /* Set the raw data */
      ASSIGN lvRawData = GET-BYTES(memptrCDoc, lvRecordCount, MINIMUM({&DOCUMENT_PARTITION_SIZE}, lvDocumentSize - lvRecordCount + 1)).

      /* Create the record */
      CREATE LABEL_IMAGE NO-ERROR.
      IF ERROR-STATUS:ERROR THEN
      DO:
        RUN CREATE_ERROR_RECORD(INPUT "Unable to create image record","IMAGE_IMPORT").
        LEAVE MAIN_BLOCK.
      END.
      ASSIGN
        LABEL_IMAGE.ENTITY_MNEMONIC      = "PIT"
        LABEL_IMAGE.SOURCE_OBJECT_NUMBER = PACKAGED_ITEM.PACKAGED_ITEM_NUMBER
        LABEL_IMAGE.IMAGE_NAME           = lvReference.
      &IF PROVERSION BEGINS "9" OR (DBTYPE("DB") EQ "Progress" OR DBTYPE("DB") EQ "OpenEdge") &THEN
        ASSIGN LABEL_IMAGE.IMAGE = lvRawData.
      &ELSE
         ASSIGN LABEL_IMAGE.IMAGE = STRING(lvRawData).
      &ENDIF
      VALIDATE LABEL_IMAGE NO-ERROR.
      IF ERROR-STATUS:ERROR THEN
      DO:
        RUN CREATE_ERROR_RECORD(INPUT "Unable to update image record","IMAGE_IMPORT").
        LEAVE MAIN_BLOCK.
      END.
      RELEASE LABEL_IMAGE.
    END.
    RELEASE LABEL_IMAGE.

    SET-SIZE(memptrCDoc) = 0.
    LENGTH(lvRawData) = 0.
    ASSIGN
      memptrCDoc = ?
      lvRawData  = ?.

The code to drag it out again:
Code:
  OUTPUT STREAM stImageFile TO VALUE(opFileName) BINARY NO-MAP NO-CONVERT.

  FOR EACH LABEL_IMAGE NO-LOCK
     WHERE LABEL_IMAGE.ENTITY_MNEMONIC EQ "PIT"
          AND LABEL_IMAGE.SOURCE_OBJECT_NUMBER EQ PACKAGED_ITEM.PACKAGED_ITEM_NUMBER:

    PUT STREAM stImageFile CONTROL LABEL_IMAGE.IMAGE.
  END.

  OUTPUT STREAM stImageFile CLOSE.

Now while the image file that is pulled back out again is loaded fine in a Crystal XI report, when we try and view the image in a normal image viewer we get messages to say it is corrupt. I've attached before and after files. The after is stored as a zip on here as it won't upload!!

Anyone have any bright ideas?!
 

Attachments

  • before.jpg
    before.jpg
    82.2 KB · Views: 8
  • after.zip
    after.zip
    80.5 KB · Views: 8
try

COPY-LOB FROM ABEL_IMAGE.IMAGE TO FILE FileName APPEND NO-CONVERT.

instead

PUT STREAM stImageFile CONTROL LABEL_IMAGE.IMAGE.

and

COPY-LOB FROM FILE FileName Starting AT BytesFrom FOR CopyBytes TO LABEL_IMAGE.IMAGE NO-CONVERT. /* for storing image part */
 
Hmmm hitting some problems because the datatype of LABEL_IMAGE.LABEL is RAW. COPY-LOB doesn't like that. As far as I understand we need to store it as a RAW so we can run the system on an SQL back end.
 
We've solved the problem - the chunks weren't being pulled back out in the right order. Not sure how Crystal was recognising it as a valid image though. Anyhow - we're cooking on gas now.
 
We are on 10.1B and our databases are still v9 dbs as we have v9 customers. Very annoying.
 
Back
Top