Posted Wed, 26 Aug 2015 10:11:52 GMT in reply to
Louie Bacani Foronda
Reply
On Wed, 26 Aug 2015 03:06:37 GMT, Louie Bacani Foronda wrote:
On Tue, 25 Aug 2015 18:13:37 GMT, Louie Bacani Foronda wrote:
how do I send a file to a visitor?
example if I visit 127.0.0.1:8080/getfile/15ca691e-f886-41e0-abfd-3616655905fb.zip from the browser I should be able to download the file..
is there a response function which I can use?
This is what I am trying to achieve example:
void getFile(HTTPServerRequest req, HTTPServerResponse res){
string file = std.path.buildNormalizedPath("inventory/files/"~req.file);
res.download(file, "200");
// the file is downloaded and a 200 server response is sent as a header
}
I figured it out...
(...)
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.