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

发布于 2024-08-26 11:46:16 字数 103 浏览 8 评论 0原文

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

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

ぺ禁宫浮华殁 2024-09-02 11:46:16

试试这个:

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

Try this:

require 'open-uri'
open('image.png', 'wb') do |file|
  file << open('http://example.com/image.png').read
end
泛泛之交 2024-09-02 11:46:16

更短的版本:

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

要保持相同的文件名:

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

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]}")
云归处 2024-09-02 11:46:16

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

假设您有类似的内容:

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

在您的模型上,只需将图像指定为 URL,类似这样的内容(用故意的手写体编写):

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

您可能希望将其放入模型中的方法中。这在 Heroku 的临时文件系统上也能正常工作。

回形针将从那里取出它。

来源:回形针文档

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

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):

@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.

Paperclip will take it from there.

source: paperclip documentation

假装不在乎 2024-09-02 11:46:16

我认为这是最明确的方法:

require 'open-uri'

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

I think this is the clearest way:

require 'open-uri'

File.write 'image.png', open('http://example.com/image.png').read
水溶 2024-09-02 11:46:16

可能是最简单的方法:

require 'open-uri'
image_url = "https://i.imgur.com/ZWnhY9T.png"
IO.copy_stream(URI.open(image_url), 'destination.png')

Possibly the simplest way:

require 'open-uri'
image_url = "https://i.imgur.com/ZWnhY9T.png"
IO.copy_stream(URI.open(image_url), 'destination.png')
携余温的黄昏 2024-09-02 11:46:16

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

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

流年里的时光 2024-09-02 11:46:16

使用 Ruby 3 及更高版本,您将使用接受的答案得到以下错误:

没有这样的文件或目录@ rb_sysopen - http://example.com/image.png (错误号::ENOENT)

解决方案是使用 URI.open 代替 Kernel.open。例子:

require "uri"

download = URI.open('http://example.com/image.png')
File.write('~/image.png', download)

Using Ruby 3 and above, you'll get the following error using the accepted answer:

No such file or directory @ rb_sysopen - http://example.com/image.png (Errno::ENOENT)

The solution is to use URI.open in place of the Kernel.open. Example:

require "uri"

download = URI.open('http://example.com/image.png')
File.write('~/image.png', download)
东京女 2024-09-02 11:46:16

上面的例子都很棒。
就我而言,我只想从 URL 中的图像创建下载链接。

如果您想让它可下载(到您的下载文件夹),您可以在控制器中使用以下代码:

require 'open-uri'
file_type = url.to_s.split(".")[-1]

send_data open(url).read, filename: "some_name.#{file_type}", type: "image/#{file_type}", disposition: "attachment"

All above examples are great.
In my case I just wanted to create a download link from the image from URL.

If you want to make it downloadable (to your downloads folder), you can use the following code in your controller:

require 'open-uri'
file_type = url.to_s.split(".")[-1]

send_data open(url).read, filename: "some_name.#{file_type}", type: "image/#{file_type}", disposition: "attachment"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文