downloading and storing files from given url to given path in lua

前端 未结 1 1088
感情败类
感情败类 2021-01-19 07:47

I\'m new with lua but working on an application that works on specific files with given path. Now, I want to work on files that I download. Is there any lua libraries or lin

1条回答
  •  时光说笑
    2021-01-19 08:20

    You can use the LuaSocket library and its http.request function to download using HTTP from an URL.

    The function has two flavors:

    • Simple call: http.request('http://stackoverflow.com')
    • Advanced call: http.request { url = 'http://stackoverflow.com', ... }

    The simple call returns 4 values - the entire content of the URL in a string, HTTP response code, headers and response line. You can then save the content to a file using the io library.

    The advanced call allows you to set several parameters like HTTP method and headers. An important parameter is sink. It represents a LTN12-style sink. For storing to file, you can use sink.file:

    local file = ltn12.sink.file(io.open('stackoverflow', 'w'))
    http.request {
        url = 'http://stackoverflow.com',
        sink = file,
    } 
    

    0 讨论(0)
提交回复
热议问题