Reputation: 304
In python tornado server,I got a file of video(like:test.mp4) from client's request. And the type of file is 'tornado.httputil.HTTPFile'.
I want to save it to my server like './video/myvideo.mp4'. I know opencv videoCapture may could do that,but didn't use the APIs well.
Upvotes: 0
Views: 348
Reputation: 21779
You can save files by using Python's built-in open()
function, like:
with open('myfile.txt', 'w') as f:
f.write('Hello world')
A simple Tornado example:
class UploadHandler(tornado.web.RequestHandler):
# ...
def post(self):
for field_name, files in self.request.files.items():
for info in files:
filename = info['filename'] # name of the file
# NOTE: as pointed out by Ben Darnell, if user submitted
# filename contains special characters like "../",
# it poses a security risk. You should generate your
# own filenames. See `uuid.uuid4()`.
body = info['body'] # contents of the file
with open('video/%s' % filename, 'w') as f:
f.write(body)
self.write('Upload successful')
NOTE: If the uploaded files are large, you might face some problems. Read this - issue on GitHub. You can, however, use tornado.web.stream_request_body
decorator to circumvent this issue, although I have neither any experience with that nor a working code example.
Upvotes: 1