Phone: +65-8585-1750

Address: 30 Holland Close, Singapore

Pylons File Upload

Sun, Jan 30, 2011 mdda OSS Blog Comments

Pylons File Upload

This code snippet should clear up the confusing documentation when it comes to uploading an optional picture :

import os
from pylons import config
import shutil

class MyController(BaseController):
    def picture(self, registration=False):
        if 'send_picture' in request.params:
            if ('picture' in request.params) and hasattr(request.params['picture'], 'filename'): 
                picture = request.params['picture']
                log.debug("GOT PICTURE ! '%s'" % (picture.filename, ))
                profile.picture = "%d-%s" % (user.id, picture.filename.replace(os.sep, '_'), )
                
                permanent_file = open(
                 os.path.join(
                  config['app_conf']['picture_store'],
                  profile.picture,
                 ),
                 'wb'
                )
                shutil.copyfileobj(picture.file, permanent_file)
                picture.file.close()
                permanent_file.close()

Tags:
pylons python

blog comments powered by Disqus