Ano*_*ous 5 lua download luasocket
我正在尝试创建更新程序,所以当我的Lua应用程序已过期时,它将使用LuaSocket下载较新的.exe文件(可以运行我的Lua代码).
在这个更新程序中,我希望它显示到目前为止已经下载了多少.但是,使用以下HTTP请求,它会阻止应用程序直到完全下载:
local b, c, h = http.request("https://www.example.com/Download/Example.exe?from="..Game.Version)
Run Code Online (Sandbox Code Playgroud)
我正在使用线程下载它,但是在线程内部完成下载之前我仍然无法写入文件,因此进度条将为0%,100%,之间没有任何内容.
我可以做些什么来下载远程文件,但在下载时将其保存到本地文件中?
cURL可以做到这一点.我不知道LuaSocket或Lua的其他任何东西都可以.:(
Mic*_*man 10
你说得对 - cURL可以做到.LuaSocket没有此功能.您可以创建一个LTN12接收器,它将报告所取得的进展,但在完全下载之前您不会知道该文件的总大小,因此它有点无用.为什么不使用luacurl呢?
local curl = require "luacurl"
local c = curl.new()
function GET(url)
c:setopt(curl.OPT_URL, url)
local t = {} -- this will collect resulting chunks
c:setopt(curl.OPT_WRITEFUNCTION, function (param, buf)
table.insert(t, buf) -- store a chunk of data received
return #buf
end)
c:setopt(curl.OPT_PROGRESSFUNCTION, function(param, dltotal, dlnow)
print('%', url, dltotal, dlnow) -- do your fancy reporting here
end)
c:setopt(curl.OPT_NOPROGRESS, false) -- use this to activate progress
assert(c:perform())
return table.concat(t) -- return the whole data as a string
end
local s = GET 'http://www.lua.org/'
print(s)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7577 次 |
| 最近记录: |