我正在尝试与python一起使用ajaxupload:http://valums.com/ajax-upload/

我想知道如何使用Python访问上传的文件。在网站上,它说:

* PHP: $_FILES['userfile']
* Rails: params[:userfile]

Python的语法是什么?

request.params ['userfile']似乎不起作用。

提前致谢!这是我当前的代码(使用导入作为图像的PIL)

im = Image.open(request.params['myFile'].file)
有帮助吗?

解决方案

在Django中,您可以使用:

request.FILES['file']

代替:

request.POST['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.

我正在与金字塔合作,我试图做同样的事情。一段时间后,我提出了这个解决方案。

from cStringIO import StringIO
from cgi import FieldStorage

fs = FieldStorage(fp=request['wsgi.input'], environ=request)
f = StringIO(fs.value)

im = Image.open(f)

我不确定这是否是“正确”的,但似乎可以正常工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top