Ajaxuploadを使用したPython画像アップロード
-
19-09-2019 - |
質問
AjaxuploadをPythonで使用しようとしています。http://valums.com/ajax-upload/
アップロードされたファイルにPythonを使用してアクセスする方法を知りたいと思います。 Webサイトでは、次のように書かれています。
* PHP: $_FILES['userfile']
* Rails: params[:userfile]
Pythonの構文は何ですか?
request.params ['userfile']は機能していないようです。
前もって感謝します!これが私の現在のコードです(画像としてインポートされたPILを使用)
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)
それが「正しい」ものかどうかはわかりませんが、うまくいくようです。
所属していません StackOverflow