Skip to content Skip to sidebar Skip to footer

Pylons Uploading Distorted Images On Windows

I'm creating an web app in Pylons, and am working on an image upload action. This is currently running using egg:paste#http on my windows machine, in the basic development configu

Solution 1:

You are probably just missing the 'b' (binary) flag for effectively writing the file as binary:

save_file = open(os_path, 'wb')

But I don't see why you need the shutil.copyfileobj call in there, why not do something like this:

file_save_path = os.path.join(config.images_dir, request.POST['image'].filename)
file_contents = request.POST['image'].file.read()

# insert sanity checks here...

save_file = open(file_save_path, 'wb')
save_file.write(file_contents)
save_file.close()

Or make the last three lines a bit more pythonic (making sure the file handle gets closed even if the write fails):

withopen(file_save_path, 'wb') as save_file:
    save_file.write(file_contents)

It's possible you need a

from __future__ import with_statements

at the top of your file if you're below Python 2.6.

Post a Comment for "Pylons Uploading Distorted Images On Windows"