#How to return static files by names?

1 messages · Page 1 of 1 (latest)

sonic delta
#
from pathlib import Path
from litestar.response import File
from litestar import Litestar, get
from litestar.static_files import create_static_files_router

@get("/")
async def start() -> str:
    return "home"

@get("/photo")
async def photo() -> File:
    return File(
        path=app.route_reverse("static", file_path="amd.jpg"),
        content_disposition_type="inline",
    )

app = Litestar(
    route_handlers=[
        start, photo,
        create_static_files_router("/static", [Path(__name__).resolve().parent / "static"])
    ],
    debug=True,
)
.
├── app.py
└── static
    └── amd.jpg

Why I get 500: /static/amd.jpg does not exist

mystic sandBOT
#
Notes for How to return static files by names?
At your assistance

@sonic delta

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

spiral fern
#

what do you see when you set html_mode = True

#

and browse to /static?

hasty stump
#

May I kindly suggest you read the docs on static files? 🙂

#

This is not how they are work in general

#

create_static_files_router creates a router that serves static files

#

You are trying to use the path, that is the HTTP path, of that router as an argument to File, which tries to read that path from disk