web.py multipart file handling

来源:互联网 发布:淘宝限制购买数量 编辑:程序博客网 时间:2024/05/21 17:43

I'll have to admit that... it was so easy to use $_FILE for file uploading in PHP.


Trying to switch to Python from PHP (for various reasons including shared configuration between web and background processes, etc). Started looking at web.py (with other alternatives as well) and, surprisingly, found that the webpy's document is nothing more than... nothing. Even the "receipts" in "cookbook" are for those who don't need them. So I'm lost totally just in handling uploaded file.


Task: to handle a file upload.

Solution:

def POST(self):  x = web.input('log', log={})

Trick here is the log={} part. Without it, x.log would be a string containing the whole content of the file. This is a completely undocumented behavior. If you need something more familiar, specify the "log={}" part, and you'll get x.log.filename for remote filename and x.log.file for a file object to access it. I still haven't figured out how to directly operate on the uploaded file (or python didn't do this part at all?) but at least I got a file object to deal with.


Related code:

https://github.com/webpy/webpy/blob/master/web/utils.py#L160

If the key is not in the "defaults" , or the specified defaults value isn't a dict, value is inflated before added to the Storage object.



原创粉丝点击