将文件从给定的 url 下载并存储到 lua 中的给定路径 [英] downloading and storing files from given url to given path in lua
问题描述
我是 lua 新手,但正在开发一个可以处理具有给定路径的特定文件的应用程序.现在,我想处理我下载的文件.是否有任何 lua 库或代码行可用于下载并将其存储在我的计算机上?
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 line of codes that I can use for downloading and storing it on my computer ?
推荐答案
您可以使用 LuaSocket 库及其 http.request
函数使用 HTTP 从 URL 下载.
You can use the LuaSocket library and its http.request
function to download using HTTP from an URL.
该函数有两种风格:
- 简单调用:
http.request('http://stackoverflow.com')
- 高级调用:
http.request { url = 'http://stackoverflow.com', ... }
这个简单的调用返回 4 个值 - 字符串中 URL 的全部内容、HTTP 响应代码、标头和响应行.然后,您可以使用 io
将内容保存到文件中 图书馆.
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.
高级调用允许您设置多个参数,例如 HTTP 方法和标头.一个重要的参数是sink
.它代表一个 LTN12-style sink.要存储到文件,您可以使用 sink.file
:
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,
}
这篇关于将文件从给定的 url 下载并存储到 lua 中的给定路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!