Dmitry Bubnenkov
Dmitry Bubnenkov

Reputation: 9859

How to load files to local file system with vibed?

I need to send data from web-browser to local FS. For sending data I am using Vue-JS component

<file-upload class="my-file-uploader" name="myFile" id="myCustomId" action="/upload" multiple>Inside Slot Text</file-upload>

My server side based on vibed. But I can't find example how to save binary data to local FS.

router.any("/upload", &upload);    
...
void upload(HTTPServerRequest req, HTTPServerResponse res)
{

}

It's seems that I should use HTTPServerRequest.files But I can't understand how to use it. User upload takes is multiple files.

Upvotes: 3

Views: 249

Answers (1)

greenify
greenify

Reputation: 1127

You can find a lot of examples within the Vibe.d Github repository.

For example there's a small uploader.

router.post("/upload", &uploadFile);

...   

void uploadFile(scope HTTPServerRequest req, scope HTTPServerResponse res)
{
    auto pf = "file" in req.files;
    enforce(pf !is null, "No file uploaded!");
    try moveFile(pf.tempPath, Path(".") ~ pf.filename);
    catch (Exception e) {
        logWarn("Failed to move file to destination folder: %s", e.msg);
        logInfo("Performing copy+delete instead.");
        copyFile(pf.tempPath, Path(".") ~ pf.filename);
    }

    res.writeBody("File uploaded!", "text/plain");
}

I don't know much about Vue.js, but it seems they use file too.

Upvotes: 4

Related Questions