Last active
May 8, 2025 16:50
-
-
Save AalbatrossGuy/0e59751db10088c4fd69fc87033ec3cd to your computer and use it in GitHub Desktop.
Uploading Large Files in Chunks via Flask
This file contains hidden or 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
@app.route("/upload", methods=["GET", "POST"]) | |
def upload(): | |
if request.method =="POST": | |
for file in request.files: # Get the file using flask.request from your <input> tag | |
if file.startswith('file'): | |
get_file = request.files.get(file) # When uploading multiple files, use the loop | |
with open(f"{os.getenv('ROOT_DIRECTORY')}/{get_file.filename}", 'wb') as file_binary: # wb will create the file if it doesn't exist and write the chunk | |
for file_chunk in get_file.stream: # Get the file in a stream reader | |
file_binary.write(file_chunk) | |
return render_template("upload.html") |
This file contains hidden or 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
<!-- <!DOCTYPE html> --> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="stylesheet" href="../static/css/style.css"> | |
<title>Large File Upload Example</title> | |
</head> | |
<body> | |
<form action="/upload" method="POST" enctype="multipart/form-data"> | |
<input type = "file" name = "file" accept = "*"> | |
<button type="submit">SUBMIT</button> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment