| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:求助,lua如何获取multipart表单参数
只看楼主 加入收藏
编程@菜鸟
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2023-2-7
收藏
 问题点数:0 回复次数:0 
求助,lua如何获取multipart表单参数
if string.sub(receive_headers["content-type"],1,20) == "multipart/form-data;" then   
                        is_have_file_param = true
                        content_type = receive_headers["content-type"]
                        --body_data可是符合http协议的请求体,不是普通的字符串
                        body_data = ngx.req.get_body_data()

                        --请求体的size大于nginx配置里的client_body_buffer_size,则会导致请求体被缓冲到磁盘临时文件里,client_body_buffer_size默认是8k或者16k
                        if not body_data then
                                local datafile = ngx.req.get_body_file()
                                if not datafile then
                                        error_code = 1
                                        error_msg = "no request body found"
                                else
                                        local fh, err = io.open(datafile, "r")
                                        if not fh then
                                                error_code = 2
                                                error_msg = "failed to open " .. tostring(datafile) .. "for reading: " .. tostring(err)
                                        else
                                                fh:seek("set")
                                                body_data = fh:read("*a")
                                                fh:close()
                                                if body_data == "" then
                                                        error_code = 3
                                                        error_msg = "request body is empty"
                                                end
                                        end
                                end
                        end
                        local new_body_data = {}
                        --确保取到请求体的数据
                        if not error_code then
                                local boundary = "--" .. string.sub(receive_headers["content-type"],31)
                                local body_data_table = explode(tostring(body_data),boundary)
                                local first_string = table.remove(body_data_table,1)
                                local last_string = table.remove(body_data_table)
                                for i,v in ipairs(body_data_table) do
                                        local start_pos,end_pos,capture,capture2 = string.find(v,'Content%-Disposition: form%-data; name="(.+)"; filename="(.*)"')
                                        --普通参数
                                        if not start_pos then
                                                local t = explode(v,"rnrn")
                                                local temp_param_name = string.sub(t[1],41,-2)
                                                local temp_param_value = string.sub(t[2],1,-3)
                                                args[temp_param_name] = temp_param_value
                                        else
                                         --文件类型的参数,capture是参数名称,capture2是文件名                           
                                                file_args[capture] = capture2
                                                table.insert(new_body_data,v)
                                        end
                                end
                                table.insert(new_body_data,1,first_string)
                                table.insert(new_body_data,last_string)
                                --去掉app_key,app_secret等几个参数,把业务级别的参数传给内部的API
                                body_data = table.concat(new_body_data,boundary)--body_data可是符合http协议的请求体,不是普通的字符串
                        end
只支持txt等基本文件,无法适用于压缩文件

[此贴子已经被作者于2023-2-7 13:23编辑过]

搜索更多相关主题的帖子: if 参数 local then end 
2023-02-07 11:57
快速回复:求助,lua如何获取multipart表单参数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015388 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved