如何使用Lua通过Web上传文件?

如何使用 Lua 编程语言从浏览器上传文件?

我正在使用 Orbit Web 框架。

原文链接 https://stackoverflow.com/questions/6159964

点赞
stackoverflow用户513763
stackoverflow用户513763

这个示例直接来自于 orbit 示例页面/test.op。

<form method="POST" enctype="multipart/form-data" action="test.op">
    <input type="file" name="file">
    <input type="submit" value="Upload">
</form>

$lua{[[
    local f = web.input.file
    upload = {}
    if f then
        local name = f.name
        local bytes = f.contents
        local dest = io.open(web.real_path .. "/" .. name, "wb")
        if dest then
            dest:write(bytes)
            dest:close()
        upload[1] = name
        end
    end
]]}

您可以轻松地将其适应为常规的orbit post handler。您还可以查看我如何在我的library project中使用它,但我猜它比你的典型用法要复杂得多。

2011-05-28 08:33:21