I’m trying to upload images with the following Python CGI function:
def save(image):
path = '%s/%s' % ('dir/images', image.filename)
nfile = open(path, 'wb')
while 1:
chunk = image.file.read(2<<16)
if not chunk:
break
nfile.write(chunk)
nfile.close()
It works fine with SVGs and text files, but when I try to upload an image (jpeg, png and gif tested), the resulting file cannot be displayed because it “contains errors”. And it seems that the image is, indeed, not completely uploaded.
I really do not understand why it works with SVGs and TXTs, but not with images.
Thanks a lot in advance!
After trying my script on Debian, I found that my problem was definitely due to python for Windows, since it worked just fine on Debian.
To avoid getting truncated files when uploading binary files on Windows, you have to use the -u flag:
And it should fix the truncated file problem.
http://mail.python.org/pipermail/python-list/2007-December/439231.html