How to load files to local file system with vibed?

别说谁变了你拦得住时间么 提交于 2019-12-11 00:32:59

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/37911439/how-to-load-files-to-local-file-system-with-vibed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!