使用Faraday Ruby gem下载图像并写入磁盘

时间:2022-12-02 20:56:40

When using the Faraday gem to hit a URL for an image like this:

使用法拉第宝石来搜索图像的URL时,如下所示:

http_conn = Faraday.new do |builder|
  builder.adapter Faraday.default_adapter
end 
response = http_conn.get 'http://example.tld/image.png'

How can you write the image file from the Faraday response to disk?

如何将图像文件从法拉第响应写入磁盘?

Debugging the response.body attribute shows it is binary data.

调试response.body属性显示它是二进制数据。

Any help greatly appreciated.

任何帮助非常感谢。

1 个解决方案

#1


13  

Why not just write the response.body like any other file?

为什么不像任何其他文件一样编写response.body?

File.open('image.png', 'wb') { |fp| fp.write(response.body) }

#1


13  

Why not just write the response.body like any other file?

为什么不像任何其他文件一样编写response.body?

File.open('image.png', 'wb') { |fp| fp.write(response.body) }