Reputation: 1007
I wrote this code:
class uploadfromfile:
def POST(self, name=None):
filename = ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for i in range(20))
x = web.input(upfile={})
f = open(filename, 'w')
f.write(x['upfile'].value)
f.close()
imgFromFile(filename)
return "some html"
But it doesn't work. I get a huge error that ends with this: UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 15: invalid start byte
The error appears to arrive at f.write(x['upfile'].value), and I can't for the life of me figure out why. Any ideas what is going wrong? I know that the value is in that variable, because if I just return it the image displays in my browser.
Upvotes: 0
Views: 242
Reputation: 111
The first thing I can see wrong with the code is that the file isn't opened in binary mode. When reading at writing files that aren't simple strings, binary mode is required to treat the data as nothing more than bytes. Simply switch the file opening like to f = open(filename, 'wb')
to resolve that issue.
Upvotes: 1