Hi,

Web.py-Noob here :-)
I am trying to upload images to a web.py 0.31 instance through a
simple form template (along the lines of the examples given in
http://webpy.org/cookbook/fileupload and http://webpy.org/cookbook/storeupload
Only difference is that the form is in a seperate template, not
defined in the main web.py code.

On submitting the form, I receive an UnicodeDecodeError:

Traceback (most recent call last):
[...]
  File "/usr/lib/python2.5/site-packages/web.py-0.31-py2.5.egg/web/
utils.py", line 123, in unicodify
    if _unicode and isinstance(s, str): return safeunicode(s)
  File "/usr/lib/python2.5/site-packages/web.py-0.31-py2.5.egg/web/
utils.py", line 231, in safeunicode
    return obj.decode(encoding)
  File "/usr/lib/python2.5/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:
unexpected code byte

I am aware that this issue has been discussed before (eg in
http://groups.google.com/group/webpy/browse_thread/thread/a828612a5e51004b/1e1c6af57e9b14cd
). I have also tried the approach described in http://webpy.org/cookbook/forms
- to no avail but a slightly different sounding unicode error
traceback.

I'd appreciate any feedback on this issue.

Thanks,
Mats

# the class calling the form:

class imgupload:
    def GET(self):
        # parse GET variable
        id = web.input().id
        self.id = id
        self.thinsection = db.select('thinsections', where="id=%s" %
id)[0]
        self.imglist = db.select('images', where='thinsections_id=%s'
% id)
        return render.imgupload(self.thinsection, self.imglist)

    def POST(self):
        x = web.input(myfile={})
        filedir = tspath + self.thinsection.id  # change this to the
directory you want to store the file in.
        web.debug(filepath)
        if 'myfile' in x: # to check if the file-object is created
            filepath=x.myfile.filename.replace('\\','/') # replaces
the windows-style slashes with linux ones.
            filename=filepath.split('/')[-1] # splits the and chooses
the last part (the filename with extension)
            fout = file(filedir +'/'+ filename,'w') # creates the file
where the uploaded file should be stored
            fout.write(x.myfile.file.value) # writes the uploaded file
to the newly created file.
            fout.close() # closes the file, upload complete.
        raise web.seeother('/detail?id=%s' % self.id)

# the template:

$def with (thinsection, imglist)

$# set page title
$var title: $thinsection.name

<h4>upload new image for '$thinsection.name'</h4>

<form method="POST" enctype="multipart/form-data" action="">
<input type="file" name="myfile" />
<br/>
<input type="submit" />
</form>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to