简体   繁体   中英

Rails, paperclip, uploading a photo from a rake task?

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?

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("http://i.stack.imgur.com/qjKuQ.jpg")

which resulted in:

=> #<File:/var/folders/Zo/ZoJYH-A6Eg8GQ3pV0fIyhU+++TU/-Tmp-/open-uri20101117-5813-1h64t5k>

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

这是我认为的一种黑客攻击,但我只是在我的迁移中使用ActionDispatch :: Http :: UploadedFile.new对象复制文件并像这样运行它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM