0

我还是 Vibe.d 的新手,所以如果我遗漏了一些明显的东西,请原谅我。

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

Vibe.d 文档是一项很好的工作,我对此表示赞赏,但直到现在它还相当稀少,而且示例很少而且相差甚远。

以下是我的一些代码片段:

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 的另一种方法?

非常感谢!

4

3 回答 3

1

我完全忘记了这个问题。我记得当您无法轻易找到对那些已经知道的人来说似乎很基本的问题的答案时,这是多么令人沮丧。

确保在表单的 enctype 中声明“multipart/form-data”:

form(method="post", action="new_employee", enctype="multipart/form-data")

然后该表单中的字段应包含“文件”类型的输入字段,如下所示:

input(type="file", name="picture")

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

auto pic = "picture" in request.files;

这是一个示例 postNewEmployee() 方法被传递一个 Employee 结构:

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

希望您觉得这个有帮助。

于 2021-03-14T16:03:31.717 回答
0

你可以试试hunt-framework,Hunt Framework 是一个高级 D 编程语言 Web 框架,它鼓励快速开发和干净、实用的设计。它使您可以快速轻松地构建高性能 Web 应用程序。

操作示例代码:

    @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;
    }
于 2019-11-22T08:22:10.970 回答
0

把上传函数放到WebApp类里面,用它来处理表单postform(action="/upload", method ="post")

class WebApp {

    addUpload(HTTPServerRequest req, ...)
    {
        auto file = file in req.files;
        ...
    }
}
于 2017-01-20T14:52:48.400 回答