【问题标题】:How to load files to local file system with vibed?如何使用 vibed 将文件加载到本地文件系统?
【发布时间】:2016-06-19 20:17:53
【问题描述】:

我需要将数据从网络浏览器发送到本地 FS。为了发送数据,我使用 Vue-JS component

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

我的服务器端基于 vibed。但我找不到如何将二进制数据保存到本地 FS 的示例。

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

}

看来我应该使用HTTPServerRequest.files 但我不明白如何使用它。用户上传需要多个文件。

【问题讨论】:

    标签: d vibed


    【解决方案1】:

    您可以在 Vibe.d Github repository 中找到很多示例。

    例如有一个小的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");
    }
    

    我对 Vue.js 了解不多,但 it seems 他们也使用 file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多