首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用web框架上载vibe.d中的文件

如何使用web框架上载vibe.d中的文件
EN

Stack Overflow用户
提问于 2016-12-17 16:36:45
回答 3查看 630关注 0票数 0

如果我错过了一些显而易见的事情,请原谅我。

我想使用web框架在Vibe.d中上传一个文件。但是,我发现的所有例子,包括书中的“D Web开发”,都没有使用web框架。如果我将非web框架示例插入到我的应用程序中,它就会崩溃。如果我不得不为了一个功能,即文件上传而放弃web框架,那就太糟糕了。

Vibe.d文档是一项很好的工作,我对此表示赞赏,但到目前为止,它相当稀少,而且示例很少。

下面是我的代码片段:

代码语言:javascript
复制
shared static this()
{
    auto router = new URLRouter;
    router.post("/upload", &upload);
    router.registerWebInterface(new WebApp);
    //router.get("/", staticRedirect("/index.html"));
    //router.get("/ws", handleWebSockets(&handleWebSocketConnection));
    router.get("*", serveStaticFiles("public/"));

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

    conn = connectMongoDB("127.0.0.1");
    appStore = new WebAppStore;
}

void upload(HTTPServerRequest req, HTTPServerResponse res)
{
    auto f = "filename" in req.files;
    try
    {
        moveFile(f.tempPath, Path("./public/uploaded/images") ~ f.filename);
    }
    catch(Exception e) 
    {
        copyFile(f.tempPath, Path("./public/uploaded/images") ~ f.filename);
    }
    res.redirect("/uploaded");
}

我仍然可以使用web框架访问HTTPServerRequest.files吗?多么?还是我还需要它?也就是说,不使用HTTPServerRequest.files还有其他方法吗?

非常感谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-01-20 14:52:48

将上传函数放入WebApp类中,并使用它处理表单post form(action="/upload", method ="post")

代码语言:javascript
复制
class WebApp {

    addUpload(HTTPServerRequest req, ...)
    {
        auto file = file in req.files;
        ...
    }
}
票数 0
EN

Stack Overflow用户

发布于 2021-03-14 16:03:31

我完全忘记了这个问题。我记得当你很难找到一个对那些已经知道的人来说是最基本的问题的答案时,这是多么令人沮丧。

请确保在表单的封装类型中声明“多部分/表单-数据”:

代码语言:javascript
复制
form(method="post", action="new_employee", enctype="multipart/form-data")

然后,该形式的字段应该包含一个类型为“file”的输入字段,如下所示:

代码语言:javascript
复制
input(type="file", name="picture")

在web框架类的postNewEmployee()方法中,通过request.files获取文件:

代码语言:javascript
复制
auto pic = "picture" in request.files;

下面是正在传递一个Employee结构的示例postNewEmployee()方法:

代码语言:javascript
复制
void postNewEmployee(Employee emp)
{
    Employee e = emp;
    string photopath = "No photo submitted";
    auto pic = "picture" in request.files;
    if(pic !is null) 
    {
        string ext = extension(pic.filename.name);
        string[] exts = [".jpg", ".jpeg", ".png", ".gif"];
        if(canFind(exts, ext))
        {
            photopath = "uploads/photos/" ~ e.fname ~ "_" ~ e.lname ~ ext;
            string dir = "./public/uploads/photos/";
            mkdirRecurse(dir);
            string fullpath = dir ~ e.fname ~ "_" ~ e.lname ~ ext;
            try moveFile(pic.tempPath, NativePath(fullpath));
            catch (Exception ex) copyFile(pic.tempPath, NativePath(fullpath));
        }
    }
    e.photo = photopath;
    empModel.addEmployee(e);
    redirect("list_employees");
}

当我再次尝试学习Vibe.d时,我再次意识到缺乏教程,所以我自己写了一个教程,而作为一个学习者,一切都是新鲜的:

https://github.com/reyvaleza/vibed

希望你觉得这个有用。

票数 1
EN

Stack Overflow用户

发布于 2019-11-22 08:22:10

您可以尝试亨特-框架,亨特框架是一种高级的D编程语言网络框架,它鼓励快速开发和干净、实用的设计。它允许您快速轻松地构建高性能的Web应用程序。

行动代码示例:

代码语言:javascript
复制
    @Action
    string upload()
    {
        string message;

        if (request.hasFile("file1"))
        {
            auto file = request.file("file1");

            if (file.isValid())
            {
                // File save path: file.path()
                // Origin name: file.originalName()
                // File extension: file.extension()
                // File mimetype: file.mimeType()

                if (file.store("uploads/myfile.zip"))
                {
                    message = "upload is successed";
                }
                else
                {

                    message = "save as error";
                }
            }
            else
            {
                message = "file is not valid";
            }
        }
        else
        {
            message = "not get this file";
        }

        return message;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41200754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档