Store png/JSImage in PostgreSQL

Using Servoy to administrate the content of your website? Discuss all webrelated Servoy topics on this forum!

Store png/JSImage in PostgreSQL

Postby dfrazier » Tue Jul 31, 2012 8:56 pm

I want to create a program that allows a user to upload an image (png) and have it stored in my PostgreSQL database. I already created a table/column for it. Table name is "images". I set the datasource of the form to this table. There are 2 columns. id (integer) and imagedata (bytea)(bytea is the column type Servoy creates for MEDIA). Then I created a button that will open a "showFileOpenDialog". When the user selects a file and then presses "upload", the callback function is called. The callback function sets the mediaDataProvider to the JSImage. This mediaDataProvider is used by a media element on the form. The image shows up on-screen in this media element as expected. Then the function tries to store the JSImage data to the PostgreSQL database.

I can never get the image data to work from the database. I am not sure if it is having trouble writing TO the database or getting the image data FROM the database. I have been able to read/write everything else from the database just fine, just not image data. Here is the code.

function buttonOnClick(event)
{
/** @type {JSFile} */
plugins.file.showFileOpenDialog(1, path, false, "*.*", callbackFunc, "Pick an image to upload")
}

function callbackFunc(jsfileArray)
{
var image1 = plugins.images.getImage(jsfileArray[0].getBytes())
mediaDataProvider = image1 //this makes the image appear in a media element successfully. No problems yet.

/** @type {JSRecord<db:/junkdata/images>} */
var record = foundset.newRecord(false)
record.imagedata = image1.getData() //Is this what is failing?
databaseManager.saveData(record)
}

Then I have another button on the form that will trigger this code:
mediaDataProvider = plugins.images.getImage(foundset.imagedata) //This NEVER works. The media element goes blank. Anyone have some advice?
This is my signature.
User avatar
dfrazier
 
Posts: 40
Joined: Fri Jun 29, 2012 3:43 pm
Location: Earth

Re: Store png/JSImage in PostgreSQL

Postby achiary » Mon Aug 06, 2012 4:46 am

Try the following :

foundset.newRecord(false)
foundset.imagedata = image1.getData()

instead of :

var record = foundset.newRecord(false)
record.imagedata = image1.getData() //Is this what is failing?
achiary
 
Posts: 83
Joined: Wed Nov 14, 2007 3:29 pm
Location: Argentina

Re: Store png/JSImage in PostgreSQL

Postby matthewbrice » Fri Aug 10, 2012 6:09 am

I believe that the below is your problem.
var record = foundset.newRecord(false)
record.imagedata = image1.getData() //Is this what is failing?


foundset.newrecord(false) returns the index to the newly created record, not the record object itself. Perhaps a better way is :

var record = foundset.getrecord( foundset.newrecord(false) )
record.imagedata = ...
Matt Brice
matthewbrice
 
Posts: 36
Joined: Wed Jun 13, 2012 6:42 pm


Return to Web Development

Who is online

Users browsing this forum: Bing [Bot] and 9 guests