Performance: File download responses
This affects page load speed and user experience by controlling how files are sent from the server to the browser for download.
from flask import Flask, send_file app = Flask(__name__) @app.route('/download') def download(): return send_file('largefile.zip', conditional=True, as_attachment=True)
from flask import Flask, send_file app = Flask(__name__) @app.route('/download') def download(): return send_file('largefile.zip')
| Pattern | Memory Usage | Response Start Time | Network Load | Verdict |
|---|---|---|---|---|
| Loading entire file in memory | High (loads full file) | Delayed (waits for full load) | High (single large transfer) | [X] Bad |
| Streaming file with conditional=True | Low (streams chunks) | Fast (starts immediately) | Optimized (chunked transfer) | [OK] Good |
as_attachment=True parameter do when used with Flask's send_file function?send_file usagesend_file function sends files from the server to the client in Flask.as_attachment=Trueas_attachment=True tells the browser to download the file instead of trying to open or display it.as_attachment=True means download file [OK]as_attachment=True.send_file('report.pdf', as_attachment=True), which is the correct syntax.return send_file('data.csv', as_attachment=True, download_name='user_data.csv')download_name parameterdownload_name parameter sets the filename the browser suggests when saving the file.download_name='user_data.csv', so the browser will suggest this name.return send_file('image.png', attachment=True)as_attachment=True, not attachment=True.as_attachment=True to force download instead of display.download_name='User_Manual_2024.pdf' to specify the filename suggested to the user.filename.Pass the test and this pattern moves unknown