使用 LuaSocket 上传图像

我正在尝试使用luaSocket上传图像。

这是我的Lua代码:

function uploadFile(dir)
     local resp = {}
     local body,code,headers,status = http.request{
     url = "my_url",
     method = "POST",
     headers = {
        ["Content-Type"] = "application/x-www-form-urlencoded",
        ["Content-Length"] = file_size
     },
     source = ltn12.source.file(io.open(dir),"rb"),
     sink = ltn12.sink.table(resp)
     }
     print(body,code,status)
     if headers then for k,v in pairs(headers) do print(k,v) end end end

我的php代码是:

<?php
copy("php://input","test");
echo("OK");
?>

当我尝试上传图像时,我不会收到任何错误,但body和status都是nil,但code是“timeout”。 但是,如果我尝试上传文本文件,则脚本正常工作。

任何帮助都将不胜感激。谢谢。

点赞
用户1190388
用户1190388

你正在将 "rb" 作为参数传递到 ltn12.sink.file 中,而不是 io.open。将语句修改为:

source = ltn12.source.file( io.open(dir,"rb") ),
2015-08-19 21:00:20