我有一个简短的问题。是否可以保存文件而不实际通过表单上传?
例如,假设我正在查看电子邮件中的附件,并且我想使用回形针保存它们。我该怎么做呢?我是否必须在某处手动调用 save_file(或类似的东西)?
任何帮助将非常感激!
我有一个 rake 任务,可以将目录中的图像(客户端徽标)直接加载到 parperclip 上。您也许可以根据您的需要进行调整。
这是我的简化客户端模型:
class Client < ActiveRecord::Base
LOGO_STYLES = {
:original => ['1024x768>', :jpg],
:medium => ['256x192#', :jpg],
:small => ['128x96#', :jpg]
}
has_attached_file :logo,
:styles => Client::LOGO_STYLES,
:url => "/clients/logo/:id.jpg?style=:style"
attr_protected :logo_file_name, :logo_content_type, :logo_size
然后在我的 rake 任务中我这样做:
# the logos are in a folder with path logos_dir
Dir.glob(File.join(logos_dir,'*')).each do |logo_path|
if File.basename(logo_path)[0]!= '.' and !File.directory? logo_path
client_code = File.basename(logo_path, '.*') #filename without extension
client = Client.find_by_code(client_code) #you could use the ids, too
raise "could not find client for client_code #{client_code}" if client.nil?
File.open(logo_path) do |f|
client.logo = f # just assign the logo attribute to a file
client.save
end #file gets closed automatically here
end
end
Regards!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)