PostgreSQLLargeObject

Has anyone has saved images in POSTGRES and read the picture to display them on a web page?

I just want the reading process using PostgreSQLLargeObject.

i not used it but i made notes

typ oid
xojo methods for this objects
https://docs.xojo.com/PostgreSQLDatabase
https://docs.xojo.com/PostgreSQLDatabase.CreateLargeObject
DeleteLargeObject
OpenLargeObject

https://docs.xojo.com/PostgreSQLLargeObject

Thanks Markus.

I review all documentation.
But I didn’t find anything related to the use of the web picture object.

I only found a method that does this in the EddiesElectronics method, in the load customer picture, as I show:

Var photoName As String = "user_" + Str(mCurrentCustomerID)
If PhotoCache = Nil Then PhotoCache = New Dictionary
If Not PhotoCache.HasKey(photoName) Then
  Var wp As WebPicture = pic
  wp.Filename = photoName + ".png"
  wp.MIMEType = "image/png"
  wp.UseCompression = False
  PhotoCache.Value(photoName) = wp
End If
PhotoImage.Picture = PhotoCache.Value(photoName)

But this example doesn’t use PostgreSQLLargeObject.

usually images are requested via url.
should be an event, there you load and return/response data with correct mime type.
this way is for normal web pages.

webpicture have a constructor for picture or memory block.

If you are storing the picture in a database you can skip the WebPicture part. If you need to send it to the browser too, just use the pic that you are assigning to the WebPicture. You can get the raw data using the Picture.ToData method.

https://docs.xojo.com/Picture.ToData

2 Likes

Thank you very much, Greg !!!
Your response was very timely.

I already solved the problem, using the Picture.ToData method. Just change it to another method that does the opposite.

Regards,

I copy the solution, for someone who has the same problem:

Session.DBlink.SQLExecute("BEGIN TRANSACTION")

Var lo As PostgreSQLLargeObject = session.DBlink.OpenLargeObject(oid)
lo.Position = 0

Var losize As Integer= lo.Length
Var pic As Picture
Var wp As WebPicture = pic

// Utilizamos un tipo MIME genérico, podemos ser específicos sobre el tipo de documento
// a descargar
wp.MIMEType = "image/png" // puede utilizar uno más específico por tipo de documento https://developer.mozilla.org/es/docs/Web/HTTP/Basics_of_HTTP/MIME_types
wp.Filename = filename
wp.UseCompression = False
MyWebImageViewer_Object.Picture = Picture.FromData(lo.Read(losize))

Session.DBlink.SQLExecute("END TRANSACTION")

thats the case you use it direct in xojo at controls

for my understanding it is shorter because i can’t see the use of WebPicture in your example above.

db.ExecuteSQL("BEGIN TRANSACTION")

Var lo As PostgreSQLLargeObject = session.DBlink.OpenLargeObject(oid)

MyWebImageViewer_Object.Picture = Picture.FromData(lo.Read(lo.Length))

Session.DBlink.SQLExecute("END TRANSACTION")

No. He has to provide the mime type and the file name in a web project.

BTW: don’t do this unless you’re getting errors. You’re only hurting yourself and your users with this.

1 Like

Great advice. Probably, it is really more convenient to create a separate database for storing images in order to have quick access to them and not do unnecessary work on image processing.

Thanks Greg.

Do you recommend deleting the line related to UseCompression ?

but where was wp variable used???

I do.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.