RejectedSoftware Forums

Sign up

How to send a file to the browser.

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
}

Re:File Download.

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...

void downloadFile(HTTPServerRequest req, HTTPServerResponse res){
	string s = std.path.buildNormalizedPath(std.file.getcwd()~"/data/inventory/15ca691e-f886-41e0-abfd-3616655905fb.epub");
	string filename = std.path.baseName(s);
	ubyte[] file = cast(ubyte[])std.file.read(s);
	res.writeBody(file, 200, "application/epub+zip");
}

Re:File Download.

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.

Re:File Download.

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);
}

Re:File Download.

On Thu, 27 Aug 2015 16:20:44 GMT, Louie Bacani Foronda wrote:

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);
}

I haven't tried this, but adding the following before the call to serveStaticFile should work:

res.headers["Content-Disposition"] = "attachment; filename=\"custom-name.epub\"";