4

How can I upload a file with paperclip via the console or a in a rake task? I'm a little unsure how this works without a form. Has anyone dealt with this?


update

So I found this:

image = Image.new(:storage => File.open('/path/to/my/image.png', rb))

But I guess this makes my question a little simpler, say my photo was actually online (at varying domains) and I wanted to take it with paperclip and transform it and upload it to my server. Is it possible to somehow give File.open a url?

3 Answers 3

6

Give this a shot.

require 'open-uri'
image = Image.new(:storage => open("http://path.to.the/image.png"))

Worked when I tried it in my terminal I just did:

require 'open-uri'
image = open("https://i.stack.imgur.com/qjKuQ.jpg")

which resulted in:

=> #<File:/var/folders/Zo/ZoJYH-A6Eg8GQ3pV0fIyhU+++TU/-Tmp-/open-uri20101117-5813-1h64t5k>
4
  • Hugo is right here, you just need to pass a File object to the accessor setup on your model.
    – Ryan Bigg
    Nov 17, 2010 at 21:55
  • Ryan, I think the problem I have is that I'm using S3 as my storage., any Ideas? Nov 18, 2010 at 0:42
  • Finnaly this method solved my problem to move photos from other platform! But there is still one problem left. Using this method, paperclip saves photo without extension, because it gets it from the temp file. How to fix this? Thanks! Mar 24, 2013 at 19:32
  • Oh I have found it here stackoverflow.com/questions/11523171/…. Thanks for the first steps! Mar 24, 2013 at 19:35
1

However, it saves the files as "open-uri12345sdf-1301fp." (without extension) in database. How we save original file name in photo_file_name field.

1

This is kind of a hack I think but I'd just replicated the files with ActionDispatch::Http::UploadedFile.new object within my migration and ran it like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.