Domanda

Sto cercando di utilizzare AjaxUpload con Python: http://valums.com/ajax-upload/

Mi piacerebbe sapere come accedere al file caricato con Python. Sul sito web, dice:

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

Qual è la sintassi per Python?

request.params [ 'userfile'] non sembra funzionare.

Grazie in anticipo! Qui è il mio codice attuale (usando PIL importato come immagine)

im = Image.open(request.params['myFile'].file)
È stato utile?

Soluzione

in Django, è possibile utilizzare:

request.FILES['file']

invece che:

request.POST['file']

non sapevo come fare in tralicci ... forse è lo stesso concetto ..

Altri suggerimenti

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.

Sto lavorando con la piramide, e stavo cercando di fare la stessa cosa. Dopo qualche tempo mi si avvicinò con questa soluzione.

from cStringIO import StringIO
from cgi import FieldStorage

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

im = Image.open(f)

Non sono sicuro se è il "giusto", ma sembra funzionare.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top