If you want to pass the exact file path (aka return report.docx)

Use send_file(exactpath) instead of send_from_directory

Check the documentation here https://flask.palletsprojects.com/en/2.1.x/api/

Answer from Winmari Manzano on Stack Overflow
🌐
Beautiful Soup
tedboy.github.io › flask › generated › flask.send_from_directory.html
flask.send_from_directory — Flask API
@app.route('/uploads/<path:filename>') def download_file(filename): return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
Discussions

python - Unable to retrieve files from send_from_directory() in flask - Stack Overflow
I have a html file which references static object like this Hence the browser tries to call this via and fl... More on stackoverflow.com
🌐 stackoverflow.com
python - Flask: send_from_directory - Stack Overflow
I am trying to convert json to csv and download a file from my flask application. The function does not work correctly, I always get the same csv, even if I delete the json file. Why? button: More on stackoverflow.com
🌐 stackoverflow.com
Problem to get a file using send_from_directory in Flask
It seems to work for me with pictures, videos, music, etc. Are you running the dev server in debug mode? What filenames are you passing? More on reddit.com
🌐 r/learnpython
8
August 6, 2015
Using send_file on an entire directory
Perhaps I'm mistaken, but I don't think there's a web/browser concept of downloading a directory. You need to ZIP up the directory then serve the ZIP file. More on reddit.com
🌐 r/flask
2
8
December 24, 2021
🌐
ProgramCreek
programcreek.com › python › example › 65747 › flask.send_from_directory
Python Examples of flask.send_from_directory
If a directory is requested, its index.html is sent. """ if hasattr(sys, 'frozen'): basedir = os.path.realpath(os.path.dirname(sys.executable)) data_dir = os.path.join(basedir, 'end2end', 'data') else: basedir = os.path.join(os.path.realpath(os.path.dirname(__file__)), '..') data_dir = os.path.join(basedir, 'data') print(basedir) if os.path.isdir(os.path.join(data_dir, path)): path += '/index.html' return flask.send_from_directory(data_dir, path)
🌐
HotExamples
python.hotexamples.com › examples › flask › - › send_from_directory › python-send_from_directory-function-examples.html
Python send_from_directory Examples, flask.send_from_directory Python Examples - HotExamples
def test_send_from_directory_bad_request(self, app, req_ctx): app.root_path = os.path.join( os.path.dirname(__file__), "test_apps", "subdomaintestmodule" ) with pytest.raises(BadRequest): flask.send_from_directory("static", "bad\x00")
🌐
Reddit
reddit.com › r/learnpython › problem to get a file using send_from_directory in flask
r/learnpython on Reddit: Problem to get a file using send_from_directory in Flask
August 6, 2015 -

I am trying to send a file from a Python/Flask server using

@app.route('/media/<filename>')
def send_foo(filename):
	return send_from_directory('/media/usbhdd1/downloads/', filename)

but when I click it it stucks and after a time it returns The proxy did not receive a valid response in time.

When the file is a .txt file like file.txt it works perfectly. But for binary files it does not work.

How can I solve it?

Find elsewhere
🌐
Pythonise
pythonise.com › series › learning-flask › sending-files-with-flask
Sending files with Flask | Learning Flask Ep. 14 | pythonise.com
February 18, 2019 - # The absolute path of the directory containing images for users to download app.config["CLIENT_IMAGES"] = "/mnt/c/wsl/projects/pythonise/tutorials/flask_series/app/app/static/client/img" # The absolute path of the directory containing CSV files for users to download app.config["CLIENT_CSV"] = "/mnt/c/wsl/projects/pythonise/tutorials/flask_series/app/app/static/client/csv" # The absolute path of the directory containing PDF files for users to download app.config["CLIENT_PDF"] = "/mnt/c/wsl/projects/pythonise/tutorials/flask_series/app/app/static/client/pdf" Now that we've updated our app config, let's go ahead and create our routes (I'd recommend using a config file for this which you can read more about here). The send_from_directory function is the recommended secure way to allow a user to download a file from our application.
🌐
Medium
medium.com › @01one › create-the-simplest-static-file-download-server-with-python-flask-bd6ea3950a56
Create the simplest static file download server with python flask | by Mashiur Rahman | Medium
September 10, 2025 - project-directory/ │ ├── app.py └── static/ └── image.jpg · The python script app.py · # app.py from flask import Flask, send_from_directory app = Flask(__name__) @app.route('/<filename>') def download_file(filename): # The directory where the files are located directory = 'static' # Flask's send_from_directory to send the file to the client return send_from_directory(directory, filename, as_attachment=True) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True) After running the program it will show like this in the terminal or cmd ·
🌐
Reddit
reddit.com › r/flask › using send_file on an entire directory
r/flask on Reddit: Using send_file on an entire directory
December 24, 2021 -

Hello! I'm wondering if it is possible to let the user download a directory/folder using send_file. When I try to pass in a directory instead of a file to send_file, I get a permission error in Visual Studio Code.

Edit: Solved! Here's how I did it in case anyone ever has this issue.

If you make the folder into a zip file (using shutil.make_archive for example) then you can pass in the zip file to flask.send_file() and it'll work.

Thank you u/weebsnore for your help!

🌐
KooR.fr
koor.fr › Python › API › web › flask › send_from_directory.wp
KooR.fr - Fonction send_from_directory - module flask - Description de quelques librairies Python
Programmation Python Les compléments Voir le programme détaillé · Vous êtes un professionnel et vous avez besoin d'une formation ? RAG (Retrieval-Augmented Generation)et Fine Tuning d'un LLM Voir le programme détaillé Module « flask » · def send_from_directory(directory: 'os.PathLike[str] | str', path: 'os.PathLike[str] | str', **kwargs: 't.Any') -> 'Response'
🌐
Reddit
reddit.com › r/flask › problem retreiving uploaded files with send_from_directory
r/flask on Reddit: Problem retreiving uploaded files with send_from_directory
October 5, 2020 -

Hi everyone,

For a project, I architectured my Flask app as a package. So at the root, I'm having a config.py
file where I declare the uploads folder like this:

UPLOAD_FOLDER = 'myapp/uploads'

Now from myapp/routes.py, I manage to upload files in the right folder when creating a post, but then I try to declare a route to retrieve these files with:

@app.route('/uploads/')  

def upload(filename): return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

But when displaying the html page where I call url_for('upload', filename=post.picture) I get a 404 on the image... like if Flask was trying to access /myapp/uploads from /myapp

Does someone have an idea?

I tried to hardcode "uploads" into the first parameter of send_from_directory with success. like this:

@app.route('/uploads/<filename>')
def upload(filename):
    return send_from_directory('uploads', filename)

But it's dirty and will break if I ever change the UPLOAD_FOLDER value in config.py. So I'm looking for a clean solution to this.

🌐
Python Forum
python-forum.io › thread-32178.html
Flask, send_from_directory not working : solved
January 26, 2021 - Hi everyone, here my simple test code : def download(): try: return send_from_directory('C:\\original.jpg', filename='original.jpg', as_attachment=True) except FileNotFoundError: abort(404)No matter w
🌐
Reddit
reddit.com › r/flask › confused about path arguments for file.save() vs send_from_directory()??
r/flask on Reddit: Confused about path arguments for file.save() vs send_from_directory()??
October 20, 2021 -

So I'm messing with a Flask app that uses file uploading and downloading, but I really don't understand the paths.

Here's my project's file structure: https://imgur.com/a/vydd92E

So to save a file, I use file.save(os.path.join("app/UPLOADS", filename))

Which is to say, go into app dir, then into UPLOADS dir.

But to download a file, I use send_from_directory("UPLOADS", filename).

This doesn't make sense to me. One is acting like we're outside of the app directory, the other is acting like we're already in it. How can it be both??

Like both of these lines are in the same file, routes.py, so shouldn't paths to the UPLOADS folder be the same no matter what is being done? And I understand it, the Flask app itself is operating out of the root folder, so I feel like both paths should be app/UPLOADS, but apparently not??? So confused!

routes.py in case you want more context:

from app import app
from flask import render_template, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
import os
import pdf2image
POPPLER_PATH = "app/poppler-21.10.0/Library/bin"


@app.route("/notmine", methods=["GET", "POST"])
def upload_file():
    if request.method == "POST":
        print(f"\n\n{len(request.files)}\n\n")
        # check if the post request has the file part
        if "file" not in request.files:
            flash("No file part")
            return redirect(request.url)
        file = request.files["file"]
        # If the user does not select a file, the browser submits an
        # empty file without a filename
        if file.filename == "":
            flash("No selected file")
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join("app/UPLOADS", filename))
    return render_template("upload_file.html")

def allowed_file(filename):
    return "." in filename and filename.rsplit(".", 1)[1].lower() in app.config["ALLOWED_EXTENSIONS"]

@app.route("/uploads/<name>")
def download_file(name):
    return send_from_directory("UPLOADS", name)
🌐
Workroom Productions
workroom-productions.com › serving-a-directory-with-flask
Publishing a Directory with Flask
October 18, 2022 - ## Flask server to serve a folder as a webpage ### intended to allow me to run test coverage on the commandline in replit, and to see output as html ## Set up to use the right parts from flask ### `send_from_directory` serves a file from a directory ### I could use `render_template`, but I've got no templates.
🌐
Reddit
reddit.com › r/learnpython › confused about path arguments for file.save() vs send_from_directory() in flask app??
r/learnpython on Reddit: Confused about path arguments for file.save() vs send_from_directory() in Flask app??
October 20, 2021 -

So I'm messing with a Flask app that uses file uploading and downloading, but I really don't understand the paths.

Here's my project's file structure: https://imgur.com/a/vydd92E

So to save a file, I use file.save(os.path.join("app/UPLOADS", filename))

Which is to say, go into app dir, then into UPLOADS dir.

But to download a file, I use send_from_directory("UPLOADS", filename).

This doesn't make sense to me. One is acting like we're outside of the app directory, the other is acting like we're already in it. How can it be both??

Like both of these lines are in the same file, routes.py, so shouldn't paths to the UPLOADS folder be the same no matter what is being done? And I understand it, the Flask app itself is operating out of the root folder, so I feel like both paths should be app/UPLOADS, but apparently not??? So confused!

routes.py in case you want more context:

from app import app
from flask import render_template, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
import os
import pdf2image
POPPLER_PATH = "app/poppler-21.10.0/Library/bin"


@app.route("/notmine", methods=["GET", "POST"])
def upload_file():
    if request.method == "POST":
        print(f"\n\n{len(request.files)}\n\n")
        # check if the post request has the file part
        if "file" not in request.files:
            flash("No file part")
            return redirect(request.url)
        file = request.files["file"]
        # If the user does not select a file, the browser submits an
        # empty file without a filename
        if file.filename == "":
            flash("No selected file")
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join("app/UPLOADS", filename))
    return render_template("upload_file.html")

def allowed_file(filename):
    return "." in filename and filename.rsplit(".", 1)[1].lower() in app.config["ALLOWED_EXTENSIONS"]

@app.route("/uploads/<name>")
def download_file(name):
    return send_from_directory("UPLOADS", name)
🌐
W3docs
w3docs.com › python
How to serve static files in Flask | W3Docs
from flask import Flask, send_from_directory app = Flask(__name__) @app.route('/static/<path:path>') def serve_static(path): return send_from_directory('static', path) if __name__ == '__main__': app.run(debug=True) <div class="alert alert-info flex not-prose"> Watch a video course Python - The Practical Guide</div>
🌐
PyTutorial
pytutorial.com › flask-send_from_directory-serve-files-securely-from-directories
PyTutorial | Flask send_from_directory: Serve Files Securely from Directories
November 15, 2024 - This approach is more secure than directly exposing your file system through static folders. from flask import Flask, send_from_directory app = Flask(__name__) # Route to serve files from a specific directory @app.route('/downloads/