Ruby-on-rails 使用 Carrierwave 从 Rails 控制台上传远程文件 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16918588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading a remote file url from Rails Console with Carrierwave
提问by Wasabi Developer
I just wanted to know how one would go about uploading a remote file url using Carrierwave in the Rails console.
我只是想知道如何在 Rails 控制台中使用 Carrierwave 上传远程文件 url。
I tried the following without any luck. I presume it's not processing the Uploader?
我尝试了以下没有任何运气。我认为它没有处理上传器?
user = User.first
user.remote_avatar_url = "http://www.image.com/file.jpg"
user.save
Many thanks
非常感谢
回答by Koos Rompestomper
Take a look at the 'Uploading files from a remote location' section on this page https://github.com/carrierwaveuploader/carrierwave
查看此页面上的“从远程位置上传文件”部分https://github.com/carrierwaveuploader/carrierwave
CarrierWave should throw an error if the url of the location is invalid
如果位置的 url 无效,CarrierWave 应该抛出错误
2.1.3 :015 > image.remote_image_url = "http"
=> "http"
2.1.3 :016 > image.save!
(0.2ms) BEGIN
(0.2ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Image trying to download a file which is not served over HTTP
Or if it's an unknown host:
或者如果是未知主机:
2.1.3 :017 > image.remote_image_url = "http://foobar"
=> "http://foobar"
2.1.3 :018 > image.save!
(0.4ms) BEGIN
(0.4ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Image could not download file: getaddrinfo: nodename nor servname provided, or not known
Please also note that if you want to download remote images you should prefix the attribute with remote_and suffix it with _url, as shown in the example
另请注意,如果您想下载远程图像,您应该在属性前加上remote_后缀_url,如示例所示
回答by Oss
user = User.first
user.remote_avatar = File.open(FILE_LOCATION)
user.save
FILE_LOCATION can be
FILE_LOCATION 可以是
File.join(Rails.root, '/files/png-sample.png')
if file is found in a folder 'files' in rails project
如果在 rails 项目的文件夹“files”中找到文件
回答by Subhash Chandra
I was facing the same problem. and the issue might be http is redirecting to https. So I replaced them using gsub as follows:
我面临着同样的问题。问题可能是 http 正在重定向到 https。所以我用 gsub 替换了它们,如下所示:
image.remote_image_url = remote_image_url.gsub('http://','https://')
image.save!
this should most probably solve the problem.
这应该最有可能解决问题。
回答by hellion
I have had problems with remote_avatar_url not uploading the image or throwing any errors. For me, as far as I can tell, it was because I set the following in my model.
我遇到了 remote_avatar_url 无法上传图像或抛出任何错误的问题。对我来说,据我所知,这是因为我在我的模型中设置了以下内容。
attr_accessor :remote_avatar_url
Carrierwave covers this for you, and although I don't understand why, setting it yourself buggers things up.
Carrierwave 为您涵盖了这一点,虽然我不明白为什么,但您自己设置它会使事情变得更糟。
回答by Денис Епишкин
Is work as:
工作是:
url='http://host.domain/file.jpg'
time=Time.now.to_i.to_s
myfile=IO.sysopen("tmp/"+time+"_img."+url.split(".").last,"wb+")
tmp_img=IO.new(myfile,"wb")
tmp_img.write open(URI.encode(url)).read
if File.exist?("tmp/"+time+"_img."+url.split(".").last)
"tmp/"+time+"_img."+url.split(".").last
image = ActionDispatch::Http::UploadedFile.new(:tempfile => tmp_img, :filename => File.basename(tmp_img))
else
image=nil
end
@your_model.image=image
@your_model.save

