如何从URL下载文件并将其保存到Rails中?

时间:2022-12-04 00:34:20

I have a URL to an image which i want to save locally, so that I can use Paperclip to produce a thumbnail for my application. What's the best way to download and save the image? (I looked into ruby file handling but did not come across anything.)

我有一个要本地保存的图像的URL,这样我就可以使用Paperclip为我的应用生成一个缩略图。下载和保存图像的最佳方式是什么?(我研究了ruby文件处理,但没有发现任何问题。)

5 个解决方案

#1


267  

Try this:

试试这个:

require 'open-uri'
open('image.png', 'wb') do |file|
  file << open('http://example.com/image.png').read
end

#2


92  

An even shorter version:

一个更短的版本:

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

To keep the same filename:

保持相同的文件名:

IO.copy_stream(download, "~/#{download.base_uri.to_s.split('/')[-1]}")

#3


29  

If you're using PaperClip, downloading from a URL is now handled automatically.

如果您正在使用PaperClip,从URL下载将会自动处理。

Assuming you've got something like:

假设你有:

class MyModel < ActiveRecord::Base
  has_attached_file :image, ...
end

On your model, just specify the image as a URL, something like this (written in deliberate longhand):

在您的模型中,只需将图像指定为URL,类似这样的东西(故意用长字符编写):

@my_model = MyModel.new
image_url = params[:image_url]
@my_model.image = URI.parse(image_url)

You'll probably want to put this in a method in your model. This will also work just fine on Heroku's temporary filesystem.

您可能想把它放到模型中的方法中。这在Heroku的临时文件系统上也能正常工作。

Paperclip will take it from there.

回形针会把它从那里带走。

source: paperclip documentation

来源:回形针文档

#4


7  

I think this is the clearest way:

我认为这是最清晰的方式:

require 'open-uri'

File.write 'image.png', open('http://example.com/image.png').read

#5


6  

Check out Net::HTTP in the standard library. The documentation provides several examples on how to download documents using HTTP.

请查看标准库中的Net: HTTP。该文档提供了如何使用HTTP下载文档的几个示例。

#1


267  

Try this:

试试这个:

require 'open-uri'
open('image.png', 'wb') do |file|
  file << open('http://example.com/image.png').read
end

#2


92  

An even shorter version:

一个更短的版本:

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

To keep the same filename:

保持相同的文件名:

IO.copy_stream(download, "~/#{download.base_uri.to_s.split('/')[-1]}")

#3


29  

If you're using PaperClip, downloading from a URL is now handled automatically.

如果您正在使用PaperClip,从URL下载将会自动处理。

Assuming you've got something like:

假设你有:

class MyModel < ActiveRecord::Base
  has_attached_file :image, ...
end

On your model, just specify the image as a URL, something like this (written in deliberate longhand):

在您的模型中,只需将图像指定为URL,类似这样的东西(故意用长字符编写):

@my_model = MyModel.new
image_url = params[:image_url]
@my_model.image = URI.parse(image_url)

You'll probably want to put this in a method in your model. This will also work just fine on Heroku's temporary filesystem.

您可能想把它放到模型中的方法中。这在Heroku的临时文件系统上也能正常工作。

Paperclip will take it from there.

回形针会把它从那里带走。

source: paperclip documentation

来源:回形针文档

#4


7  

I think this is the clearest way:

我认为这是最清晰的方式:

require 'open-uri'

File.write 'image.png', open('http://example.com/image.png').read

#5


6  

Check out Net::HTTP in the standard library. The documentation provides several examples on how to download documents using HTTP.

请查看标准库中的Net: HTTP。该文档提供了如何使用HTTP下载文档的几个示例。