[英]Python Image Uploading with AjaxUpload
I'm trying to use AjaxUpload with Python: http://valums.com/ajax-upload/ 我正在尝试将AjaxUpload与Python结合使用: http ://valums.com/ajax-upload/
I would like to know how to access the uploaded file with Python. 我想知道如何使用Python访问上传的文件。 On the web site, it says:
在网站上,它显示:
* PHP: $_FILES['userfile']
* Rails: params[:userfile]
What is the Syntax for Python? 什么是Python语法?
request.params['userfile'] doesn't seem to work. request.params ['userfile']似乎不起作用。
Thanks in advance! 提前致谢! Here is my current code (using PIL imported as Image)
这是我当前的代码(使用作为图像导入的PIL)
im = Image.open(request.params['myFile'].file)
import cgi
#This will give you the data of the file,
# but won't give you the filename, unfortunately.
# For that you have to do some other trick.
file_data = cgi.FieldStorage.getfirst('file')
#<IGNORE if you're not using mod_python>
#(If you're using mod_python you can also get the Request object
# by passing 'req' to the relevant function in 'index.py', like "def func(req):"
# Then you access it with req.form.getfirst('file') instead. NOTE that the
# first method will work even when using mod_python, but the first FieldStorage
# object called is the only one with relevant data, so if you pass 'req' to the
# function you have to use the method that uses 'req'.)
#</IGNORE>
#Then you can write it to a file like so...
file = open('example_filename.wtvr','w')#'w' is for 'write'
file.write(file_data)
file.close()
#Then access it like so...
file = open('example_filename.wtvr','r')#'r' is for 'read'
#And use file.read() or whatever else to do what you want.
I'm working with Pyramid, and I was trying to do the same thing. 我正在使用Pyramid,并且我正尝试做同样的事情。 After some time I came up with this solution.
一段时间后,我想出了这个解决方案。
from cStringIO import StringIO
from cgi import FieldStorage
fs = FieldStorage(fp=request['wsgi.input'], environ=request)
f = StringIO(fs.value)
im = Image.open(f)
I'm not sure if it's the "right" one, but it seems to work. 我不确定这是否是“正确”的,但似乎可行。
in django, you can use: 在Django中,您可以使用:
request.FILES['file']
instead of: 代替:
request.POST['file']
i did not know how to do in pylons...maybe it is the same concept.. 我不知道该怎么做。也许是同一概念。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.