El 30/10/13 20:54, Ricardo Aráoz escribió:

El 30/10/13 09:32, de...@stuess.de escribió:
Hi Ricardo,
do you *really* have to save Image data in db fields?
Could you not only save a link to a generic folder holding the images and only save the path of it?

If the image is in the db it will be protected by db security and permissions, if you have the image in a directory then you would have part of your security tied to the db and part of it tied to the OS permissions. I'd rather not.
There is a comment by Ed in the Wiki which says
""" Turns out some of the db modules use buffer, some use array.array, some use str to represent blobs. I just made "L" map to str, as Python's str object can handle any-length binary data just fine.""" ( http://wiki.dabodev.com/DataTypes) So may be that is what's going on, as the buffer type is mapped to a string object the framework is trying to decode it and finds some binary value that can't be decoded.


You have to encode your images in .save() (and obviously decode them before loading from db).
Hope this helps,


Thanks Frank, I'll give it a try. Problem with this is that I will have to find out some hook or method where I can perform both conversions in the Bizobj, or in the dImage (maybe I can find a hook before the controls renders the image). Or, I will have to manage without the help of having the control associated to a DataSource.
Thanks a lot.


I finally used the encoding Frank suggested.
If anyone needs to store pictures in a data base, here's how I did it:

Put encodeData() and decodeData() in a file called "encode.py" in "lib" subdirectory and made the proper imports in __init__.py (in "lib").

Then inside my main form :

        foto = ImgBtn(self,
                      RegID='EditFoto',
                      DataSource='self.Form',
                      DataField='Foto',
                      Size=(200, 200),
                      ScaleMode='Stretch',
                      DroppedFileHandler=self.Form)
        foto.onHit = self.Form.onFoto

Notice -- DataSource='self.Form' -- the DataField "Foto" is a property of my main form. Note: ImgBtn() is a class derived from dImage, it will call "onHit" method when user clicks on the picture.

    def initProperties(self):
        self.Foto = None

    def afterInitAll(self):
        self.bindEvent(dEvents.RowNumChanged, self.actualizFoto)

    def actualizFoto(self, evt):
        biz = self.getBizobj()
        self.Foto = self.Application.lib.decodeData(biz.Record.Foto)

    def onFoto(self):
        pickFile = dabo.ui.getFile('png', 'jpg', 'gif', 'bmp')
        if pickFile:
            self.EditFoto.Value = open(pickFile, "rb").read()
            encoded = self.Application.lib.encodeData(self.EditFoto.Value)
            biz = self.getBizobj()
            biz.setFieldVal('Foto', encoded)

    def processDroppedFiles(self, filelist):
        self.EditFoto.Value = open(filelist[0], "rb").read()
        encoded = self.Application.lib.encodeData(self.EditFoto.Value)
        biz = self.getBizobj()
        biz.setFieldVal('Foto', encoded)

What is going on here, I define a main form's property called "Foto", then in the image control I set the DataSource to the main form (self.Form) and the DataField to the "Foto" property. So now whenever self.Form.Foto is modified the image control will display it. Now I need to load the image into Form.Foto whenever the user moves to another record, that is done in actualizFoto() where I decode the "Foto" field before feeding it to Form.Foto. Finally I have to encode the image and assign it to the field in the db. This is done in onFoto() and processDroppedFiles(), onFoto() will fire when the user clicks on the picture, whereas processDroppedFiles() will fire when the user drops a picture on the image control.




--- StripMime Report -- processed MIME parts ---
multipart/alternative
 text/plain (text body -- kept)
 text/html
---
_______________________________________________
Post Messages to: Dabo-users@leafe.com
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/527534bd.7030...@gmail.com

Reply via email to