Vibe.d: Implementing file upload with WEB interface

aberba via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 9 11:36:18 PST 2017


So I implemented a file upload at 
https://aberba.github.io/2016/form-upload-in-vibe-d/. I'm trying 
to understand how HTTPServerRequest req.files can be accessed 
when using the web interface (Porting the same example to use 
vibe.d WEB interface).

I couldn't find the implementation details in the vibed.org docs 
either.

import vibe.d;

static this()
{
     auto router = new URLRouter;
     router.get("*", staticTemplate!"index.dt");
     router.post("/upload", &upload);

     auto settings = new HTTPServerSettings;
     settings.port = 8080;
     settings.bindAddresses = ["::1", "127.0.0.1"];
     listenHTTP(settings, router);

     logInfo("Server Running");
}

void upload(HTTPServerRequest req, HTTPServerResponse res)
{

     // File upload here
     auto file = "document" in req.files;
     try {
         moveFile(file.tempPath, Path("./public/uploads") ~ 
file.filename);
         logInfo("Uploaded successfully!");
     } catch (Exception e) {
         logInfo("Exception thrown, trying copy");
         copyFile(file.tempPath, Path("./public/uploads") ~ 
file.filename);
     }

     res.redirect("/");
}


More information about the Digitalmars-d-learn mailing list