我正在尝试使用 RubyGem Curb 构建一个文件下载器。 (看着这个问题 https://stackoverflow.com/questions/5382149/using-gem-curb-curl-to-download-file.)
我正在尝试下载一个 zip 文件,然后使用 File 类来实际创建该文件,以便我可以在 Finder 中双击它(我在 OS X 上)。我将如何将这个“卷曲”正文转换为 zip 文件。
require 'rubygems'
require 'curb'
class Download
def start
curl = Curl::Easy.new('http://wordpress.org/latest.zip')
curl.perform
curl.on_body {
|d| f = File.new('test.zip', 'w') {|f| f.write d}
}
end
end
dl = Download.new
dl.start
我没有收到任何错误,也找不到任何文件。我试过绝对路径没有区别。
我使用的是红宝石2.0
我的代码是:
curl = Curl::Easy.new('http://somepage.cz/index.html')
curl.on_body do |d|
f = File.open('output_file.html', 'w') {|f| f.write(d)}
end
curl.perform
我必须改变File.new
to File.open
没有这个它就不起作用。
移动curl.perfom
最后确实帮助了我。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)