Hi,

I've had the same issue before and did it that way:
Since your object is to upload a file and make a thumbnail at the same
time, then:

1. Hide thumbnail field in a form. User will add image only and web2py
do the rest. In your controller:

fields=['Name','MainPic']
form=SQLFORM(db.yourtable, fields=fields)

2. Create function to make thumbnail and save it into database. This
function will be called just after the form will be accepted. In your
controller:

def makeThumbnail(ImageID):
    try:    thisImage=db(db.yourtable.id==ImageID).select()[0]
    except: return
    im=Image.open(request.folder + 'uploads/' + thisImage.MainPic)
    im.thumbnail((200,150),Image.ANTIALIAS)
    thumbName='Images.ThumbPic.%s.jpg' % (uuid.uuid4())
    im.save(request.folder + 'uploads/' + thumbName,'jpeg')
    thisImage.update_record(Thumbnail=thumbName)
    return

3. Call this function. In your controller:

fields=['Name','MainPic']
form=SQLFORM(db.yourtable, fields=fields)
if form.accepts(request.vars, session):
        #form.vars.id is a id field of new record just created
        makeThumbnail(form.vars.id)

4. Do not forget to import correct modules in your controller:

from PIL import Image
import uuid


This works for me,
regards,
Lukasz

On 22 Lut, 01:26, villas <villa...@gmail.com> wrote:
> @MrFreeze Thanks for your suggestion, but unless Web2py allocates the
> filename then the default download action will not work.
> I finally got it working after eventually finding this snippet:
>
> >>> db.yourtable.insert(yourfield=db.yourtable.yourfield.store(open(filename,'rb')))
>
> My solution is to save the thumbnail to a temp.jpg file and then use
> the 'store' function to slot it into the system. It's probably a long
> way around, but it worked,  after one last obstacle...
>
> ---------------------
> @Massimo. Please take a look at sql.py 2611,  this code:
>
>     def store(self, file, filename=None, path=None):
>         if not filename:
>             filename = file.filename
>
> If you do not supply a filename,  you get an exception:
> <AttributeError: 'file' object has no attribute 'filename'.>
> Looks like a bug?

-- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.

Reply via email to