使用lua上传图片

我一直在尝试使用lua和Openresty web框架进行简单的图像上传。我找到了许多解决方案,例如

使用lua-resty-post后,我得到了表单数据,现在该怎么上传呢?

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

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

因为我是lua的新手,我不知道该使用哪个。 我的要求很简单,我需要一个文件属性并希望上传到某个地方,就像php中的move_uploaded_file一样。是否有上传文件的简单方法?

点赞
用户7234862
用户7234862

找到解决方法。使用 lua-resty-post

upload.html

<form action="/uploadimage" method="post" enctype="multipart/form-data">
  <input type="file" name="upload" accept="image/*">
  <button>上传</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",           -- 保存上传文件的路径
  name = function(name, field) -- 使用自定义函数覆盖名称
    return name.."_"..field
  end
})
local m = post:read()
2017-09-08 04:08:51