我正在尝试制作一个更新程序,以便当我的 Lua 应用程序过时时它将使用LuaSocket下载较新的 .exe 文件(可以运行我的 Lua 代码)。
在这个更新程序中,我希望它显示到目前为止已下载的数量。但是,通过以下 HTTP 请求,它会阻止应用程序,直到完全下载:
local b, c, h = http.request("https://www.example.com/Download/Example.exe?from="..Game.Version)
我正在使用线程来下载它,但是在线程内部完成下载之前我仍然无法写入文件,因此进度条将变为 0%、100%,中间没有任何内容。
我可以做些什么来下载远程文件,但在下载时将其保存到本地文件吗?
cURL 可以做到这一点。我不知道 LuaSocket 或 Lua 的其他东西是否可以。 :(
你是对的 - cURL 可以做到。 LuaSocket没有这个功能。您可以创建一个 LTN12 接收器来报告进度,但在完全下载文件之前您不会知道文件的总大小,因此它有点无用。为什么不使用luacurl http://luacurl.luaforge.net/反而?
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)