Upload image using lua

2.3k Views Asked by At

I have been trying simple image upload using lua and Openresty web framework. I found many solutions like

Using lua-resty-post I got the form data now how do I upload it?

local resty_post = require 'resty.post'
local cjson = require 'cjson'

local post = resty_post:new()
local m = post:read()
ngx.say(cjson.encode(m))

As I'm new to lua I don't understand which one to use. My requirement is very simple, I need a file attribute and want to upload on some place like in php move_uploaded_file. Is there any simple way to upload a file?

1

There are 1 best solutions below

0
On BEST ANSWER

Found the solution. Using lua-resty-post.

upload.html

<form action="/uploadimage" method="post" enctype="multipart/form-data">
  <input type="file" name="upload" accept="image/*">
  <button>Upload</button>
</form>

nginx.conf

location /uploadimage {
  content_by_lua_file image.lua;
}

image.lua

local resty_post = require "resty.post"
local post = resty_post:new({
  path = "/my/path",           -- path upload file will be saved
  name = function(name, field) -- overide name with user defined function
    return name.."_"..field 
  end
})
local m = post:read()