Alternatively, there is the built-in function
vibe.http.fileserver.serveStaticFile(s)
. It's supposed to be used in conjunction with the router:auto router = new URLRouter; router.get("/files/*", serveStaticFiles("./public/")); router.get("/some/file.zip", serveStaticFile("./some/file.zip"));
It's also possible to use them from within a method handler, just with a syntax that is a little awkward:
void downloadFile(HTTPServerRequest req, HTTPServerResponse res) { auto file = ...; serveStaticFile(file)(req, res); }
The advantage of these functions is that they handle things like cache control (ETag/Last-Modified), HEAD requests, and directly stream the file instead of first reading it into RAM.
Hello Sir, the download now is much faster... one more thing how do I change the file name of the downloaded file?
void downloadFile(HTTPServerRequest req, HTTPServerResponse res) {
string title = db.select("title","books",req.params["book_id"]);
string file = buildNormalizedPath("./data/inventory/"~req.params["book_id"]~".epub");
scope(failure) res.writeBody("File Not Found");
// check if there is a way to change the filename to title
serveStaticFile(file)(req, res);
}