Pregunta

Estoy tratando de utilizar AjaxUpload con Python: http://valums.com/ajax-upload/

Me gustaría saber cómo acceder al archivo cargado con Python. En el sitio web, que dice:

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

¿Qué es la sintaxis de Python?

request.params [ 'userfile'] no parecen funcionar.

Gracias de antemano! Aquí está mi código actual (usando PIL importado como imagen)

im = Image.open(request.params['myFile'].file)
¿Fue útil?

Solución

en Django, puede utilizar:

request.FILES['file']

en lugar de:

request.POST['file']

i no sabía cómo hacerlo en torres de alta tensión ... tal vez es el mismo concepto ..

Otros consejos

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.

Estoy trabajando con la pirámide, y yo estaba tratando de hacer lo mismo. Después de algún tiempo me ocurrió con esta solución.

from cStringIO import StringIO
from cgi import FieldStorage

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

im = Image.open(f)

No estoy seguro de si es la "correcta", pero parece que funciona.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top