简体   繁体   中英

How to decode multipart/form data sent to web2py (python)?

I am trying to send an image via HTML form to a web2py web server for storing:

   <form enctype="multipart/form-data" action="http://foo.bar.com/app/uploadimage" method="post">
        <input id="image-file" name="uploadfile" type="file" /><br>
        <input type="submit" /><br>
    </form>

The image sent to the server is encoded as multipart/form-data

In web2py (or I guess, python in general), how should I decode this data and save it as an image? Which tools are appropriate for this?

def uploadimage():    
    print request.vars.uploadfile
    # (... and now? ...)

Thank you

OK, turns out this is really simple, just do:

file = open('uploadimage.jpg', 'wb') 
file.write(request.vars.uploadfile.file.read()) 
file.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM