使用AjaxUpload上传Python图像

2024-10-01 09:16:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在Python中使用AjaxUpload: http://valums.com/ajax-upload/

我想知道如何使用Python访问上传的文件。网站上写着:

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

Python的语法是什么?在

在请求.params['userfile']似乎不起作用。在

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

^{pr2}$

Tags: 文件代码comhttp网站语法ajaxfiles
3条回答

在django中,可以使用:

request.FILES['file']

而不是:

^{pr2}$

我不知道怎么在塔架上做…也许这是同一个概念。。在

我和金字塔一起工作,我也试着做同样的事情。过了一段时间,我想出了解决办法。在

from cStringIO import StringIO
from cgi import FieldStorage

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

im = Image.open(f)

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

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.

相关问题 更多 >