Created
October 11, 2024 20:37
-
-
Save an01f01/24aba62ab06ca2c5efb122c69eeb24cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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