문제

파이썬과 함께 ajaxupload를 사용하려고합니다.http://valums.com/ajax-upload/

Python으로 업로드 된 파일에 액세스하는 방법을 알고 싶습니다. 웹 사이트에서는 다음과 같습니다.

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

파이썬의 구문은 무엇입니까?

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)

그것이 "올바른"사람인지 확실하지 않지만 효과가있는 것 같습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top