0
0
FlaskHow-ToBeginner · 3 min read

How to Send File in Flask: Simple Guide with Example

In Flask, you can send a file to the client using the send_file function from flask. This function takes the file path or file-like object and returns it as a response, allowing users to download or view the file.
📐

Syntax

The send_file function is used to send files from your Flask server to the client. It requires the file path or a file-like object as the first argument. You can also specify options like as_attachment to force download, download_name to set the filename, and mimetype to specify the file type.

  • file: Path or file object to send
  • as_attachment: Boolean to force file download (default is False)
  • download_name: Name for the downloaded file
  • mimetype: MIME type of the file (e.g., 'image/png')
python
from flask import send_file

send_file(file, as_attachment=False, download_name=None, mimetype=None)
💻

Example

This example shows a simple Flask app that sends a file named example.txt located in the same folder. When you visit /download, the file is sent as an attachment for download with a custom filename.

python
from flask import Flask, send_file

app = Flask(__name__)

@app.route('/download')
def download_file():
    # Send example.txt as a downloadable file named 'downloaded_example.txt'
    return send_file('example.txt', as_attachment=True, download_name='downloaded_example.txt')

if __name__ == '__main__':
    app.run(debug=True)
Output
When visiting http://localhost:5000/download, the browser prompts to download 'downloaded_example.txt' containing the contents of example.txt.
⚠️

Common Pitfalls

  • Not setting as_attachment=True when you want the file to download instead of display in the browser.
  • Using incorrect file paths causing FileNotFoundError.
  • Not specifying download_name if you want a custom filename for the download.
  • Serving files without proper security checks can expose sensitive files.
python
from flask import send_file

# Wrong: file will open in browser if supported
send_file('example.txt')

# Right: forces download
send_file('example.txt', as_attachment=True)

# Wrong: wrong path causes error
send_file('wrong_path.txt', as_attachment=True)

# Right: correct path and filename
send_file('example.txt', as_attachment=True, download_name='myfile.txt')
📊

Quick Reference

Use this quick reference to remember key send_file options:

OptionDescriptionDefault
filePath or file-like object to sendRequired
as_attachmentForce file download instead of inline displayFalse
download_nameFilename for the downloaded fileOriginal filename
mimetypeMIME type of the fileAuto-detected

Key Takeaways

Use Flask's send_file function to send files to clients easily.
Set as_attachment=True to force file download instead of inline display.
Always verify file paths to avoid errors and security risks.
Use download_name to customize the filename clients see when downloading.
Check MIME types if you want to control how files are handled by browsers.