从给定的URL下载和存储文件到lua中的给定路径 [英] downloading and storing files from given url to given path in lua

查看:76
本文介绍了从给定的URL下载和存储文件到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库及其

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', ... }
  • Simple call: http.request('http://stackoverflow.com')
  • Advanced call: 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样式的水槽.要存储到文件,可以使用 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屋!

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