如何在Lua中下载文件,但在写入本地文件时可以正常工作 [英] How to download a file in Lua, but write to a local file as it works

查看:1965
本文介绍了如何在Lua中下载文件,但在写入本地文件时可以正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个更新程序,所以当我的Lua应用程序是过时的,它将使用 LuaSocket 下载更新的.exe文件(可以运行我的Lua代码)。 / p>

在这个更新程序中,我希望它显示到目前为止已经下载了多少。但是,通过以下HTTP请求,它会阻止应用程序,直到完全下载:

  local b,c,h = http.request (https://www.example.com/Download/Example.exe?from=\"..Game.Version)

我正在使用线程下载它,但是我仍然无法写入文件,直到下载完成在线程内,所以进度条将会0%,100%,没有任何东西。 / p>

有什么我可以做的下载远程文件,但保存到本地文件下载?



cURL可以做到这一点。我不知道LuaSocket或其他任何Lua可以。 :(

解决方案

你是对的 - cURL可以做到这一点LuaSocket没有这个功能,你可以创建一个LTN12接收器将报告进度,但是您完全不会下载文件的总大小,所以没有用,为什么不使用 luacurl 代替?

  local curl = requireluacurl
local c = curl.new( )

函数GET(url)
c:setopt(curl.OPT_URL,url)
local t = {} - 这将收集生成的块
c:setopt curl.OPT_WRITEFUNCTION,function(param,buf)
table.insert(t,buf) - 存储收到的数据块
return #buf
end)
c:setopt( curl.OPT_PROGRESSFUNCTION,函数(param,dltotal,dlnow)
print('%',url,dltotal,dlnow) - 这里你的花哨的报告
结束)
c:setopt(curl。 OPT_NOPROGRESS,false) - 使用这个来激活e进度
assert(c:perform())
return table.concat(t) - 以字符串的形式返回整个数据
end

local s = GET'http://www.lua.org/'
打印


I'm trying to make an updater so when my Lua application is out-of-date it will use LuaSocket to download the newer .exe file (which can run my Lua code).

Inside this updater, I want it to show how much has been downloaded so far. However, with the following HTTP request, it blocks the application until downloaded fully:

local b, c, h = http.request("https://www.example.com/Download/Example.exe?from="..Game.Version)

I am using threads to download it, however I still cannot write to the file until the download is complete inside of the thread, thus the progress bar will go 0%, 100%, without anything in between.

Is there anything I can do to download a remote file, but save it to a local file as it downloads?

cURL can do this. I don't know if LuaSocket or anything else for Lua can. :(

解决方案

You are right - cURL can do it. LuaSocket does not have this functionality. You can create a LTN12 sink which will report progress made, but you won't know the total size of the file until you have downloaded it completely, so it is kind of useless. Why not use luacurl instead?

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)

这篇关于如何在Lua中下载文件,但在写入本地文件时可以正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆