如何使用 Lua 发送 multipart/form-data 格式的文件?

这是我的代码:

    http = require("socket.http")
    ltn12 = require("ltn12")
    lfs = require "lfs"

    respbody = {}

    local _start = [[--abcd]]..'\r\n'..[[Content-Disposition: form-data; name="file"; filename="test.png"]]..'\r\n'..[[Content-Type: image/jpeg]]..'\r\n\r\n'
    local _end = '\r\n'..[[--abcd--]]..'\r\n'

    local file= io.open('./test.png')

    local fileSize = lfs.attributes('./test.png').size

    local  body, code, headers, status = http.request {
    method = "POST",
    url = 'http://127.0.0.1:826/api/upload',
    headers = {
        ["Content-Type"] =  "multipart/form-data; boundary=abcd",
        ["Content-Length"] = fileSize + #_start + #_end
    },
    source = ltn12.source.cat(ltn12.source.string(_start),ltn12.source.file(file),ltn12.source.string(_end)),
    sink = ltn12.sink.table(respbody)
}

    print(body, code, headers, status, respbody)

我使用这个示例发送文件,但是并没有收到!这是结果:

enter image description here

点赞
用户7504558
用户7504558
  1. 你需要以二进制模式读取文件
  2. 有一次我放弃了使用 ltn12.source.file 而使用了 ltn12.source.string,因为文件长度不正确

像你的解决方案一样尝试我的解决方案:

PS:你还需要确保在服务器端文件字段的名称与客户端相同:basename ($ _ FILES ['file'] ['name']);

http = require("socket.http")
ltn12 = require("ltn12")
lfs = require "lfs"
http.TIMEOUT = 5

local function upload_file ( url, filename )
    local fileHandle = io.open( filename,"rb")
    if (fileHandle) then
        local fileContent = fileHandle:read( "*a" )
        fileHandle:close()
        local  boundary = 'abcd'
        local  header_b = 'Content-Disposition: form-data; name="file"; filename="' .. filename .. '"\r\nContent-Type: text/plain\r\n'
        local  fileContent =  '--' ..boundary .. '\r\n' ..header_b ..'\r\n'.. fileContent .. '\r\n--' .. boundary ..'--\r\n'
        local   response_body = { }
        local   _, code = http.request {
            url = url ,
            method = "POST",
            headers = {    ["Content-Length"] =  fileContent:len(),
                           ['Content-Type'] = 'multipart/form-data; boundary=' .. boundary
                         },
            source = ltn12.source.string(fileContent) ,
            sink = ltn12.sink.table(response_body),
                }
     return code, table.concat(response_body)
    else
     return false, "File Not Found"
    end
end

 local rc,content = upload_file ('http://127.0.0.1:826/api/upload', 'test.png' )
 print(rc,content)
2020-12-17 11:33:01