我还是 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 的另一种方法?
非常感谢!