简体   繁体   中英

Uploading an image using LuaSocket

I'm trying to upload an image using luaSocket.

Here is my Lua code:

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

My php code is:

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

When I try to upload the image I don't get any error but body and status are nil, but code is "timeout". But the script works fine if I try to upload a text file.

Any help is appreciated. Thanks.

You are passing "rb" as parameter to ltn12.sink.file instead of io.open . Change the statement to:

source = ltn12.source.file( io.open(dir,"rb") ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM