سؤال

أحاول استخدام ajaxupload مع بيثون:http://valums.com/ajax-upload/

أود أن أعرف كيفية الوصول إلى الملف الذي تم تحميله مع Python. على موقع الويب، تقول:

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

ما هو بناء جملة لبثون؟

طلب. لا يبدو أن ['userfile' يعمل.

شكرا مقدما! إليك الرمز الحالي (باستخدام PIS المستوردة كصورة)

im = Image.open(request.params['myFile'].file)
هل كانت مفيدة؟

المحلول

في Django، يمكنك استخدام:

request.FILES['file']

بدلا من:

request.POST['file']

لم أكن أعرف كيف أفعل في صلب ... ربما هو نفس المفهوم ..

نصائح أخرى

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.

أنا أعمل مع الهرم، وأنا أحاول أن أفعل الشيء نفسه. بعد بعض الوقت جئت مع هذا الحل.

from cStringIO import StringIO
from cgi import FieldStorage

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

im = Image.open(f)

لست متأكدا مما إذا كان "الحق"، ولكن يبدو أنه يعمل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top