我正在尝试将文件(音频文件)从django webapp上传到django休息服务。
表格:
<form method="post" action="/" name="submit" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="uploaded_file"><br /><br />
<input type="submit" name="submit" value="Submit">
</form>
观点:
from poster.streaminghttp import register_openers
from poster.encode import multipart_encode
import urllib2
<...>
def post(self, request):
"""
POST request: handles file upload to web service
"""
uploaded_file = request.FILES['uploaded_file']
server = "http://127.0.0.1:8000"
headers = {}
# Register the streaming http handlers with urllib2
register_openers()
data = {
'uploaded_file' : uploaded_file,
'file_name' : uploaded_file.name,
'additionalattr': 111,
}
datagen, headers = multipart_encode(data)
headers['Authorization'] = 'Token <...>'
headers['Connection']='keep-alive'
urllib_request = urllib2.Request('%s/file_upload/' % ( server ), datagen, headers)
json_response = json.loads(urllib2.urlopen(urllib_request).read())
return <...>
当我上传文件时,我会收到由multipart_encode引起的错误代码:
Traceback (most recent call last):
File "/Users/phoebus/Library/Python/2.7/lib/python/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/Users/phoebus/Library/Python/2.7/lib/python/site-packages/django/core/handlers/base.py", line 186, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/phoebus/Library/Python/2.7/lib/python/site-packages/django/core/handlers/base.py", line 184, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/phoebus/Library/Python/2.7/lib/python/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/phoebus/Library/Python/2.7/lib/python/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/phoebus/Library/Python/2.7/lib/python/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Users/phoebus/Projects/speechanalyzerwebapp/speechanalyzerwebapp/welcome.py", line 41, in post
datagen, headers = multipart_encode(data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/poster/encode.py", line 411, in multipart_encode
headers = get_headers(params, boundary)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/poster/encode.py", line 311, in get_headers
headers['Content-Length'] = str(get_body_size(params, boundary))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/poster/encode.py", line 302, in get_body_size
size = sum(p.get_size(boundary) for p in MultipartParam.from_params(params))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/poster/encode.py", line 177, in from_params
filetype=filetype, fileobj=value))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/poster/encode.py", line 108, in __init__
self.filesize = os.fstat(fileobj.fileno()).st_size
UnsupportedOperation: fileno
有人可以帮我理解出了什么问题以及如何解决它吗?
提前致谢!
答案 0 :(得分:0)
好像UploadedFile
似乎不是问题,您使用的poster
库是旧更新检查最新版本here,更新了我的旧答案
答案 1 :(得分:0)
看来我和您有同样的问题。我花了很长时间终于解决了这个问题。
这是我的代码:
import urllib2
from poster.encode import multipart_encode, MultipartParam
from poster.streaminghttp import register_openers
register_openers()
class MyView(APIView):
def post(self, request, format=None):
uploaded_file = request.FILES.get('img', None)
if uploaded_file:
new_data = {}
new_data['img'] = MultipartParam(
'img',
filename=uploaded_file.name,
filetype=uploaded_file.content_type,
filesize=uploaded_file._size, # required, if missed, will raise `UnsupportedOperation: fileno` error
fileobj=uploaded_file.file
)
datagen, headers = multipart_encode(new_data)
request = urllib2.Request(URL, datagen, headers)
response = urllib2.open(request)
if response.code==200:
print(response.read())
return 'OK'
else:
return 'error'
希望不要太晚为您提供帮助。