Skip to content

Instantly share code, notes, and snippets.

@an01f01
Created October 11, 2024 20:37
Show Gist options
  • Save an01f01/24aba62ab06ca2c5efb122c69eeb24cf to your computer and use it in GitHub Desktop.
Save an01f01/24aba62ab06ca2c5efb122c69eeb24cf to your computer and use it in GitHub Desktop.
class FileUploadHandler(BaseHandler):
"""
File upload handler inbehrits the BaseHandler, obtains file data from a POST call
"""
def post(self):
# Retrieve file from the request
fileinfo = self.request.files['file'][0]
filename = fileinfo['filename']
content_type = fileinfo['content_type']
file_body = fileinfo['body']
# Saving file to directory
output_file_path = os.path.join('uploads', filename)
with open(output_file_path, 'wb') as output_file:
output_file.write(file_body)
self.set_status(200)
self.write(f"File '{filename}' with content type '{content_type}' uploaded and saved.")
self.finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment