Bird
Raised Fist0
Flaskframework~20 mins

File download responses in Flask - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the as_attachment=True parameter do when used with Flask's send_file function?
easy
A. Encrypts the file before sending it.
B. Changes the file's content type to plain text.
C. Forces the browser to download the file instead of displaying it.
D. Prevents the file from being downloaded.

Solution

  1. Step 1: Understand send_file usage

    The send_file function sends files from the server to the client in Flask.
  2. Step 2: Role of as_attachment=True

    Setting as_attachment=True tells the browser to download the file instead of trying to open or display it.
  3. Final Answer:

    Forces the browser to download the file instead of displaying it. -> Option C
  4. Quick Check:

    as_attachment=True means download file [OK]
Hint: Remember: as_attachment=True means download, not display [OK]
Common Mistakes:
  • Thinking it changes file content type
  • Assuming it encrypts the file
  • Believing it blocks download
2. Which of the following is the correct syntax to send a file named 'report.pdf' as a download in Flask?
easy
A. return send_file('report.pdf', as_attachment=True)
B. return send_file('report.pdf', download=True)
C. return send_file('report.pdf', attachment=True)
D. return send_file('report.pdf', force_download=True)

Solution

  1. Step 1: Recall correct parameter for download

    The correct parameter to force download is as_attachment=True.
  2. Step 2: Check syntax correctness

    return send_file('report.pdf', as_attachment=True) uses send_file('report.pdf', as_attachment=True), which is the correct syntax.
  3. Final Answer:

    return send_file('report.pdf', as_attachment=True) -> Option A
  4. Quick Check:

    Correct parameter is as_attachment=True [OK]
Hint: Use as_attachment=True to force download in send_file [OK]
Common Mistakes:
  • Using incorrect parameter names like download or attachment
  • Forgetting to set as_attachment=True
  • Using non-existent parameters like force_download
3. What will be the filename suggested to the user when downloading a file with this Flask code?
return send_file('data.csv', as_attachment=True, download_name='user_data.csv')
medium
A. data.csv
B. user_data.csv
C. download_name.csv
D. No filename suggested

Solution

  1. Step 1: Understand download_name parameter

    The download_name parameter sets the filename the browser suggests when saving the file.
  2. Step 2: Analyze given code

    The code sets download_name='user_data.csv', so the browser will suggest this name.
  3. Final Answer:

    user_data.csv -> Option B
  4. Quick Check:

    download_name sets saved filename [OK]
Hint: download_name sets the saved filename for downloads [OK]
Common Mistakes:
  • Assuming original filename is always used
  • Confusing download_name with as_attachment
  • Thinking no filename is suggested
4. Identify the error in this Flask code snippet intended to send a file for download:
return send_file('image.png', attachment=True)
medium
A. The parameter should be as_attachment=True, not attachment=True.
B. The filename must be absolute path, not relative.
C. send_file cannot send image files.
D. Missing download_name parameter.

Solution

  1. Step 1: Check parameter correctness

    The correct parameter to force download is as_attachment=True, not attachment=True.
  2. Step 2: Validate other options

    Relative paths are allowed, images can be sent, and download_name is optional.
  3. Final Answer:

    The parameter should be as_attachment=True, not attachment=True. -> Option A
  4. Quick Check:

    Correct parameter is as_attachment=True [OK]
Hint: Use as_attachment=True, not attachment=True [OK]
Common Mistakes:
  • Using attachment=True instead of as_attachment=True
  • Thinking download_name is mandatory
  • Believing relative paths are invalid
5. You want to send a PDF file named 'manual.pdf' for download but want the user to save it as 'User_Manual_2024.pdf'. Which Flask code snippet correctly achieves this?
hard
A. return send_file('manual.pdf', as_attachment=True, filename='User_Manual_2024.pdf')
B. return send_file('manual.pdf', as_attachment=False, download_name='User_Manual_2024.pdf')
C. return send_file('manual.pdf', download_name='User_Manual_2024.pdf')
D. return send_file('manual.pdf', as_attachment=True, download_name='User_Manual_2024.pdf')

Solution

  1. Step 1: Ensure file is sent as download

    Set as_attachment=True to force download instead of display.
  2. Step 2: Set the saved filename

    Use download_name='User_Manual_2024.pdf' to specify the filename suggested to the user.
  3. Step 3: Check other options

    return send_file('manual.pdf', as_attachment=False, download_name='User_Manual_2024.pdf') disables download, C misses as_attachment, D uses wrong parameter filename.
  4. Final Answer:

    return send_file('manual.pdf', as_attachment=True, download_name='User_Manual_2024.pdf') -> Option D
  5. Quick Check:

    Use as_attachment=True and download_name for filename [OK]
Hint: Use as_attachment=True and download_name to rename download [OK]
Common Mistakes:
  • Forgetting as_attachment=True disables download
  • Using filename instead of download_name
  • Setting as_attachment=False causes inline display
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard