开发者

How to upload a file (via web) using Lua?

How do I upload a file, from a browser, using the Lua programming language?

I'm using the Orbit开发者_如何学编程 web framework


This sample comes straight from the orbit sample pages/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
]]}

You can easily adapt this to a normal orbit post handler. You can also take a look at how I used it in my library project, but it's way more complicated than your typical usage I guess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜