Frage

Ich versuche, AjaxUpload mit Python zu verwenden:http://valums.com/ajax-upload/

Ich würde gerne wissen, wie man mit Python auf die hochgeladene Datei zugreift. Auf der Website heißt es:

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

Was ist die Syntax für Python?

request.params ['userFile'] scheint nicht zu funktionieren.

Danke im Voraus! Hier ist mein aktueller Code (mit PIL als Bild importiert)

im = Image.open(request.params['myFile'].file)
War es hilfreich?

Lösung

In Django können Sie verwenden:

request.FILES['file']

Anstatt von:

request.POST['file']

Ich wusste nicht, wie man in Pylonen geht ... vielleicht ist es das gleiche Konzept.

Andere Tipps

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.

Ich arbeite mit Pyramide und habe versucht, dasselbe zu tun. Nach einiger Zeit habe ich mich auf diese Lösung ausgedacht.

from cStringIO import StringIO
from cgi import FieldStorage

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

im = Image.open(f)

Ich bin mir nicht sicher, ob es das "richtige" ist, aber es scheint zu funktionieren.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top