0
0
Flaskframework~20 mins

File download responses in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Download Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Flask route return?
Consider this Flask route that sends a file to the user for download. What will the user experience when accessing this route?
Flask
from flask import Flask, send_file
app = Flask(__name__)

@app.route('/download')
def download():
    return send_file('example.txt', as_attachment=True)
AThe browser will display the contents of 'example.txt' inline without download prompt.
BThe browser will prompt the user to download 'example.txt' as a file.
CThe server will return a 404 error because 'example.txt' is not found.
DThe browser will show a blank page with no content.
Attempts:
2 left
💡 Hint
The 'as_attachment=True' argument controls how the file is sent to the browser.
📝 Syntax
intermediate
2:00remaining
Which option correctly sets a custom filename for download?
You want the user to download a file but with a different filename than the original. Which Flask code snippet correctly sets the download filename to 'report.pdf'?
Flask
from flask import send_file

# Assume 'data.pdf' exists on server
Areturn send_file('data.pdf', as_attachment=True, name='report.pdf')
Breturn send_file('data.pdf', as_attachment=True, filename='report.pdf')
Creturn send_file('data.pdf', as_attachment=True, download_name='report.pdf')
Dreturn send_file('data.pdf', attachment_filename='report.pdf')
Attempts:
2 left
💡 Hint
Check the latest Flask parameter name for setting download filename.
🔧 Debug
advanced
2:00remaining
Why does this Flask file download route raise an error?
This route is intended to send a file for download but raises a TypeError. What is the cause?
Flask
from flask import send_file

@app.route('/getfile')
def getfile():
    return send_file('file.txt', as_attachment=True, download_name=123)
Aas_attachment=True is invalid and causes a syntax error.
BFlask routes cannot return send_file directly.
CThe file 'file.txt' does not exist, causing a FileNotFoundError.
Ddownload_name must be a string, not an integer, causing a TypeError.
Attempts:
2 left
💡 Hint
Check the type of the download_name parameter.
state_output
advanced
2:00remaining
What is the Content-Disposition header value sent by this Flask route?
Given this Flask route, what will be the exact Content-Disposition header value in the HTTP response?
Flask
from flask import send_file

@app.route('/dl')
def dl():
    return send_file('image.png', as_attachment=True, download_name='photo.png')
Aattachment; filename=photo.png
Binline; filename=image.png
Cattachment; filename=image.png
Dinline; filename=photo.png
Attempts:
2 left
💡 Hint
as_attachment=True sets the disposition to attachment.
🧠 Conceptual
expert
2:00remaining
Why use send_file over send_from_directory for file downloads?
Which reason best explains why you might choose Flask's send_file instead of send_from_directory when sending a file for download?
Asend_file allows sending any file-like object, not just files from a directory.
Bsend_from_directory automatically compresses files, send_file does not.
Csend_from_directory requires the file to be in the root folder, send_file does not.
Dsend_file only works with static files, send_from_directory works with dynamic files.
Attempts:
2 left
💡 Hint
Think about flexibility in file sources.