· Jul 27, 2022

Problem converting Image to Base64

Hello all, 

i am currently struggling following the instructions for reading an image file into the database as a base64 string.

My goal is to save an image into a global as base64. I followed different guides like:


Here is my current code:

 Set file=##class(%Stream.FileBinary).%New()
 Set sc=file.LinkToFile("C:\Users\fcare\Desktop\1.png")
 Do file.Rewind()
 SET BASE64=""
 While 'file.AtEnd {
  Set BASE64=BASE64_file.Read()


The resulting string however is not a valid base64 image format. I tested different images with different data types. Always the same result. Files also varied in size. 

Attached will be the original file aswell as the output for BASE64. 



Product version: Caché 2017.1
Discussion (5)1
Log in or sign up to continue

First of all, it's meaningless to post several KB of raw data. Either provide it somewhere for download or make an attachment (if possible).

Second, Base64 encodings converts 3 (incoming) bytes into 4 (outgoing) bytes, this means you always (except the last one) have to read multiple of 3 bytes at once, convert it, and put into the output stream. Your file.Read() reads 32000 bytes, which is NOT a multiple of 3!