Question
· Mar 31, 2021

Which ways i can save a photo in to the Caché or Iris Database?

I usually save the path in database like "C:\folder\picture.png", but now i want to save the photo in Iris or Caché database? Which way is better to recover the image and to maintain the original quality?

Discussion (6)1
Log in or sign up to continue

Hi

Use the %Stream.FileBinary class. A simple exmple is as follows:

Set stream=##class(%Stream.FileBinary).%New()

Set sc=stream.LinkToFile("c:\myfile.txt")

While 'stream.AtEnd { Set line=stream.Read()

; Process the chunk here

}

Typically you would read each chunk from the file into your object 'stream' and once you have reached the end of the file (AtEnd=1) you would then use the Stream Copy method to copy the stream object into your class property e.g.

Property MyPicture as %BinaryStream

and likewise you can copy the MyPicture stream into new %BinaryStream and then write that stream out to file.

Of course you can also just read from file directly into your Property MyPicture and write it out to another file or another stream object.

You don't strictly speaking need the intemediary step of reading the file contents into an instance of %BinaryStream . You can read it directly into your 'MyPicture' property however there may be cases where you might want to analyse the stream object before writing it into your class.

When you %Save() your class that contains the property 'MyPicture' the steam data is written into the 'S' global in the Cache default storage architecture. That is to say, if I have a class "Company.Staff" and for each staff member apart from their names, addresses and so on you may have indices on certain properties and you may have a property such as "StaffMemeberPicture as %Stream.FileBinary.

By the way the IRIS documentation on LinkTo() for Binary Streams warns that if the picture is edited outside of IRIS then the version of the picture stored in IRIS will be different from the edited picture external to IRIS. Hence the reason why you would read it into a intermediary %BinaryStream and then copy i into your class property. If you suspect that the external picture may have changed then if you ever export the BinaryStream back to file you might want to write it to a different filename so that you won't overwrite the edited photo and you can then compare the file size of the files to see if there is a difference which will tell you if the original has been edited. Or that's how I interpreted the documentation. 

When IRIS creates the storage definition the regular data fields go into the global ^Company.StaffD, the Indices are created in the global ^Company.StaffI and the stream data will be stored in ^Company.StaffS

Yours

Nigel

That is why we differentiate between GlobalCharacter Streams for plain text and BinaryStreams for binary data such as photo's. The reason for this is that binary data can contain characters that IRIS might interpret as terminators or have other undesirable effects and so we handle the content differently.

In Character Streams you can use any of the first 128 ascii characters except for control characters whereas Binary data can use the full 256 ASCII character set without worrying about control characters and the like.

You can of course convert the binary data into base64 encoding which will result in a pure character stream without control characters etc. Then you could use the character streams. Then when you want to export the data again your would have to do a base64 decode to get it back to pure binary (I might have got my encoding and decoding in the wrong order but the documentation will assist you with that).

Nigel