Used to process images uploaded
package.path = '/ebank/apps/openresty-web/lualib/resty/?.lua;'
local upload = require "upload"
local chunk_size = 4096
local form = upload:new(chunk_size)
local file
local filelen=0
form:set_timeout(0) -- 1 sec
local filename
function get_filename(res)
local filename = ngx.re.match(res,'(.+)filename="(.+)"(.*)')
if filename then
return filename[2]
end
end
local osfilepath = "/ebank/img/"
local i=0
while true do
local typ, res, err = form:read()
if not typ then
ngx.say("failed to read: ", err)
return
end
if typ == "header" then
if res[1] ~= "Content-Type" then
filename = get_filename(res[2])
if filename then
i=i+1
filepath = osfilepath .. filename
file,err = io.open(filepath,"w")
if not file then
ngx.say("failed to open file ")
ngx.log(ngx.ERR,err)
return
end
else
end
end
elseif typ == "body" then
if file then
filelen= filelen + tonumber(string.len(res))
file:write(res)
else
end
elseif typ == "part_end" then
if file then
file:close()
file = nil
ngx.say("file upload success")
end
elseif typ == "eof" then
break
else
end
end
if i==0 then
ngx.say("please upload at least one file!")
return
end
The document placed above the savefile.lua nginx / conf / lua / directory
nginx.conf configuration file, add the following configuration:
LOCATION / UploadFile
{
content_by_lua_file 'the conf / Lua / savefile.lua';
}
Tested successfully with the following command to upload
curl -F "[email protected]" http://127.0.0.1/uploadfile
attention lua-resty-upload module can only upload a post request body has a boundary, no boundary, then you need to use socket for transmission.