Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Lua中下载文件的最简单方法_Lua - Fatal编程技术网

在Lua中下载文件的最简单方法

在Lua中下载文件的最简单方法,lua,Lua,我的Lua应用程序发出http:request,获取JSON格式的响应,将JSON解码到表中。嗯 现在,我能够提取JSON文件中的url 但是,如何将图像url保存到Lua中的文件?有图书馆吗 编辑: 如何在Lua中获取JSON文件: json_lib = require('json') local http = libs.net.http(); local resp = http:request({ method = "get", url = "h

我的Lua应用程序发出http:request,获取JSON格式的响应,将JSON解码到表中。嗯

现在,我能够提取JSON文件中的url

但是,如何将图像url保存到Lua中的文件?有图书馆吗

编辑:

如何在Lua中获取JSON文件:

json_lib = require('json')

local http = libs.net.http();

    local resp = http:request({
        method = "get", 
        url = "http://developer.echonest.com/api/v4/artist/images?api_key=MY_API_KEY_HERE&name=zedd&format=json&results=1&start=0&license=unknown",
    }); 


    local json_full = resp.content;     

    local str_decoded = json_lib.decode(json_full)

    function RecursiveSearch(aTable)

        for key, value in pairs(aTable) do --unordered search
            if(type(value) == "table") then
                print(key,value)
                RecursiveSearch(value)
            else
                --Do something with this.
                print(key,value)
            end
        end
    end
    RecursiveSearch(str_decoded)
我的回答是:

{
                   "response":{
                      "status":{
                         "version":"4.2",
                         "code":0,
                         "message":"Success"
                      },
                      "start":0,
                      "total":20,
                      "images":[
                         {
                            "url":"http://userserve-ak.last.fm/serve/_/67419844/ZEDD.png",
                            "license":{
                               "type":"unknown",
                               "attribution":"n/a",
                               "url":"http://www.last.fm/music/ZEDD/+images"
                            }
                         }
                      ]
                   }
                }
因此,我想在磁盘上保存艺术家图片的第一个URL。

添加类似(未测试)的内容:


到脚本的结尾。

您是要保存实际图像本身还是只保存指向它的url文本链接?我想保存图像本身。我已经完成的url文本。感谢您的关注。您一定已经在使用库获取json文档了,不是吗?用它来下载图像文件不是很有效吗?正如我指出的,你已经有了一个库,可以请求你想要的图像。只需使用它,而不是将响应体视为json,将其视为所需图像的二进制数据,并将其写入文件。这是您对初始请求的响应。您的第二个请求将指向该图像URL。因此,您的响应将不是JSON文档,而是您想要获取的图像。因此,响应主体将不是json,而是图像二进制数据。把它写进一个文件。谢谢Etan Reisner最后的回答。对不起,我回来晚了。问题是我正在使用的windows程序的二进制数据处理不好。它只保存了1kb(损坏)的图像文件。我报告了错误,开发人员更新了测试版程序,问题得到了解决。现在我可以下载图片与您的上述想法。再见
local imageresp = http:request({
    method = "get", 
    url = url_from_decoded_json,
});

local imagefile = io.open("some_file", "w")
imagefile:write(resp.content)
imagefile:close()