使用AjaxUpload上传Python图像

时间:2009-10-05 02:00:14

标签: python pylons python-imaging-library image-uploading ajax-upload

我正在尝试在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)

3 个答案:

答案 0 :(得分: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.

答案 1 :(得分:1)

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

from cStringIO import StringIO
from cgi import FieldStorage

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

im = Image.open(f)

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

答案 2 :(得分:0)

在django中,您可以使用:

request.FILES['file']

而不是:

request.POST['file']

我不知道如何在挂架上做...也许它是相同的概念......