4

我正在尝试将 AjaxUpload 与 Python 一起使用:http: //valums.com/ajax-upload/

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

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

Python 的语法是什么?

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

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

im = Image.open(request.params['myFile'].file)
4

3 回答 3

1
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.
于 2009-11-09T03:39:50.567 回答
1

我正在与 Pyramid 合作,我也在尝试做同样的事情。一段时间后,我想出了这个解决方案。

from cStringIO import StringIO
from cgi import FieldStorage

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

im = Image.open(f)

我不确定它是否是“正确的”,但它似乎有效。

于 2011-01-22T18:22:49.103 回答
0

在 django 中,您可以使用:

request.FILES['file']

代替:

request.POST['file']

我不知道如何在塔里做……也许是同一个概念……

于 2009-10-05T03:37:07.120 回答