Q:

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

A:

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.

