我已经查看了很多问题,但我似乎无法找到解决我具体问题的问题。
我正在尝试允许用户(在我的Google App Engine网站上)将视频上传到wistia(视频主机)。
我可以通过直接打开文件来使其工作,但这仅在文件已经在app文件夹中时才有效:
class VideoUploadHandler(webapp2.RequestHandler):
def post(self):
title = self.request.get('title')
video = self.request.get('video')
description = self.request.get('description')
url = 'https://upload.wistia.com'
params = {
'api_password' : 'my_password',
'file' : open(video, "rb"), #<--- HOW TO USE FILE OBJECT?
'project_id' : 'my_project_id',
'name' : title,
'description' : description,
}
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
response = opener.open(url, params)
logging.warning(response.read())
self.redirect("https://www.my_webpage.com/")
如果变量'video'仅仅是文件名并且文件位于同一文件夹中,则此方法有效,但我显然需要允许用户选择任何文件,因此我将HTML更改为:
HTML:
<form enctype="multipart/form-data" action="/video_upload/" method="post">
<p>Title: <input type="text" name="title"/></p>
<p><input type="file" name="video"/></p>
<p>Description:<br><textarea rows="4" cols="50" name="description"></textarea></p>
<p><input type="submit" value="Upload"/></p>
</form>
但是,我无法理解如何在POST请求中使用文件对象。出于某种原因,我无法从已发布的其他答案中收集这些信息。
如何使用此HTML表单方法构建我的'params'?
修改
为了澄清,我现在有了这个简单的HTML:
<input id="video_input" type="file" name="video"/>
<a id="video_button">SUBMIT VIDEO</a>
$('#video_button').click(function() {
var file = new FormData();
file.append('video', $('#video_input').get(0).files[0]);
$.ajax({
type: "POST",
url: "/video_upload/",
data: file,
processData: false,
contentType: false
});
});
但我仍然没有找到任何充分解释如何获取我现在在Python中的文件对象并将其正确传递给wistia POST参数的任何内容。任何想法或链接?
修改
使用时回溯:
params = {
'api_password' : 'my_password',
'file' : video,
'project_id' : 'my_project_id',
'name' : title,
'description' : description,
}
(video.py)是带有VideoUploadHandler类的文件。
WARNING 2015-09-05 07:13:16,970 urlfetch_stub.py:504] Stripped prohibited headers from URLFetch request: ['Content-Length', 'Host']
ERROR 2015-09-05 12:13:21,026 webapp2.py:1552] HTTP Error 500: Internal Server Error
Traceback (most recent call last):
File "C:\...\webapp2-2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\...\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\...\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\...\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\...\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\...\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\...\video.py", line 84, in post
response = opener.open(url, params)
File "C:\...\urllib2.py", line 410, in open
response = meth(req, response)
File "C:\...\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "C:\...\urllib2.py", line 448, in error
return self._call_chain(*args)
File "C:\...\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\...\urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 500: Internal Server Error
INFO 2015-09-05 07:13:21,700 module.py:788] default: "POST /video_upload/ HTTP/1.1" 500 2253
答案 0 :(得分:2)
编辑:试试这个:
from poster.encode import multipart_encode, MultipartParam
from google.appengine.api import urlfetch
class VideoUploadHandler(webapp2.RequestHandler):
def post(self):
title = self.request.get('title')
description = self.request.get('description')
payload = {
'api_password' : 'my_password',
'project_id' : 'my_project_id',
'name' : title,
'description' : description,
}
file_data = self.request.POST['video']
payload['file'] = MultipartParam('file', filename=file_data.filename,
filetype=file_data.type,
fileobj=file_data.file)
data,headers= multipart_encode(payload)
send_url = "https://upload.wistia.com"
resp = urlfetch.fetch(url=send_url, payload="".join(data), method=urlfetch.POST, headers=headers)
logging.info(resp)
self.redirect("/")
改编的