Crystal lang如何从http获取二进制文件

问题描述 投票:2回答:2

在Ruby中:

require 'open-uri'
download = open('http://example.com/download.pdf')
IO.copy_stream(download, '~/my_file.pdf')

如何在水晶中做同样的事情?

http binary crystal-lang
2个回答
7
投票

我们可以做到以下几点:

require "http/client"

HTTP::Client.get("http://example.org") do |response|
  File.write("example.com.html", response.body_io)
end

这只会将没有任何HTTP标头的响应写入文件。 File.write也非常聪明,不能先将整个文件下载到内存中,而是在从给定的IO读取块时写入文件。


1
投票

我发现了一些有用的东西:

require "http/request"
require "file"
res = HTTP::Client.get "https://ya.ru"
fl=File.open("ya.html","wb")
res.to_io(fl)
fl.close
© www.soinside.com 2019 - 2024. All rights reserved.