Is there a way/will there be a way to directly display an image that is retrieved from a database? Right now the only way to display an image is by storing it in the gateway and specifying its path in a component.
It would also be helpful to know how images will be stored when the future camera functionality is implemented.
You can load a blob from a database into a paintable canvas. I donât have the code to hand but can post it later, do a search and Iâm sure it will come up as thatâs how I found it.
If youâre asking for how to do this in Perspective:
Currently, you would probably have to do it using a webdev script, that could either return the image directly or a correctly formatted base64 string, which would then be able to be used by Perspectiveâs image component (Iâm pretty sure). There are some ways that we could improve this experience, but the hard problem to deal with is the fact that the browser canât directly access the database - so we need to deliver the images to it from the gateway in a format it can understand.
Thanks @jpark this did the trick! I had to find a different way to convert to base64 as Iâm using sql server but your reply had a lot of detail which really helped me figure it out
Note that there is one heavy caveat to Jaeâs method - significantly large images will dramatically slow down page loads, and can lead to issues in the designer, as well. Image components with true URLs will be automatically asynchronously loaded by modern web browsers, in a way that image;data encoded images cannot be. For some small images, it should be fine, but be aware of that if youâre building your entire project on it.
Would you have an example of having the webdev script to to create an image data from the database and return an image.
I am fine with the SQL part of this. I have vision projects that are returning image data. I am new to the WebDev module and unsure how to return the data.
The webdev script is just return {'bytes': <your byte array>}, where your byte array is populated via whatever mechanism you prefer to query the database (ie, system.db.runNamedQuery, system.db.runPrepQuery, etc).
Try changing varbinary(max) to varchar(MAX). I think that will convert that result to varchar type which is what you want for when you convert it to the base64 string.
Probably because it wasnât needed, if the DB could do it for you. The crux of the discussion was the potential performance problems with base64 encode images, which would be avoided by serving the images via the WebDev module (direct byte stream).
Do you know how this would be accomplished in Vision? I am able to save my images using the file uploader in perspective, but then I want to display those images in the vision portion of the application, which I canât seem to find a way to do.
I did the conversion using NamedQuery:
select cast(ââ as xml).value(âxs:base64Binary(sql:column(âTable.ColVarBinaryMaxâ))â, âvarchar(max)â) as img from Pergunta where idTable = :idT
[ColVarBinaryMax] VARBINARY(max) NULL, on SQL Server.
Then I execute the function on a button >>
image3 = system.db.runNamedQuery(âQueryImgâ,{âidPâ: 14})
system.perspective.print(image3)
self.getSibling(âImageâ).props.source = âdata:imagef;base64,%sâ%image3
(âImageâ) is my component in the view on perspective
>>> import base64
>>> encoded = base64.b64encode(b'data to be encoded')
>>> encoded
b'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = base64.b64decode(encoded)
>>> data
b'data to be encoded'