www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Vibe.d: Implementing file upload with WEB interface

reply aberba <karabutaworld gmail.com> writes:
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("/");
}
Jan 09
parent aberba <karabutaworld gmail.com> writes:
On Monday, 9 January 2017 at 19:36:18 UTC, aberba wrote:

Turns out my question was answered somewhere on vibe.d website

 For those situations where more control is required, it is 
 possible to simply declare parameters of type HTTPServerRequest 
 or HTTPServerResponse to give a method full access.
Jan 11