Загрузка изображений Python с помощью AjaxUpload - PullRequest
4 голосов
/ 05 октября 2009

Я пытаюсь использовать 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)

Ответы [ 3 ]

1 голос
/ 09 ноября 2009
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 голос
/ 22 января 2011

Я работаю с Пирамидой, и я пытался сделать то же самое. Через некоторое время я придумала это решение.

from cStringIO import StringIO
from cgi import FieldStorage

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

im = Image.open(f)

Я не уверен, что это "правильный" вариант, но, похоже, он работает.

0 голосов
/ 05 октября 2009

в Django, вы можете использовать:

request.FILES['file']

вместо:

request.POST['file']

я не знал, как это сделать в пилонах ... может быть, это та же концепция ..

...