Published on

Pylons File Upload

Authors

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

python
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()