0
0
Flaskframework~20 mins

Saving uploaded files in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Upload Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Flask code saves an uploaded file?

Consider this Flask route that handles a file upload and saves it:

from flask import Flask, request
app = Flask(__name__)

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    file.save('/tmp/' + file.filename)
    return 'File saved'

What will happen if a user uploads a file named example.txt?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    file.save('/tmp/' + file.filename)
    return 'File saved'
AThe file will be saved in the /tmp directory with the name example.txt
BThe file will be saved in the current directory with the name example.txt
CThe code will raise a KeyError because 'file' is not in request.files
DThe file will be saved but with a random generated filename
Attempts:
2 left
💡 Hint

Look at the file.save line and the path used.

📝 Syntax
intermediate
2:00remaining
Which option correctly saves an uploaded file in Flask?

Which of the following Flask code snippets correctly saves an uploaded file to the 'uploads' folder?

A
file = request.files['file']
file.save('uploads/' + file.filename)
B
file = request.files.get('file')
file.save('uploads')
C
file = request.files['file']
file.save('uploads')
D
file = request.files['file']
file.save('uploads/' + file)
Attempts:
2 left
💡 Hint

Remember that file.save() needs a full path including filename.

🔧 Debug
advanced
2:00remaining
Why does this Flask file upload code raise an error?

Given this Flask route:

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    file.save('uploads/' + file.filename)
    return 'Saved'

When a user uploads a file, the server raises a FileNotFoundError. Why?

Flask
@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    file.save('uploads/' + file.filename)
    return 'Saved'
AThe file object does not have a filename attribute
BThe 'uploads' directory does not exist on the server
CThe request method is not POST
DThe file is too large to save
Attempts:
2 left
💡 Hint

Check if the folder path exists before saving files.

state_output
advanced
2:00remaining
What is the value of file.filename after upload?

In Flask, after a user uploads a file named photo.png, what is the value of file.filename in this code?

file = request.files['file']
Flask
file = request.files['file']
AAn empty string ''
B'file'
CThe full file path on the user's computer
D'photo.png'
Attempts:
2 left
💡 Hint

Think about what file.filename represents in Flask uploads.

🧠 Conceptual
expert
3:00remaining
Which option best prevents filename conflicts when saving uploaded files in Flask?

When saving uploaded files in Flask, how can you avoid overwriting existing files with the same name?

AAlways save files with their original filename in the same folder
BOverwrite existing files with the new upload to keep latest version
CGenerate a unique filename using a UUID and save the file with it
DSave files only in memory without writing to disk
Attempts:
2 left
💡 Hint

Think about how to keep filenames unique to avoid conflicts.