如何更新/重命名载波上传文件? [英] How to update/rename a carrierwave uploaded file?
本文介绍了如何更新/重命名载波上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道如何在Rails 3.2.6中使用Carrierwave-mongoid更新/重命名上载/管理的文件.我想在数据库以及文件系统中重命名该文件.
I cant figure out how to update/rename a file uploaded/managed with Carrierwave-mongoid in rails 3.2.6. I want to rename the file in the db as well as on the filesystem.
类似这样的东西...
Something like this maybe...
def rename( id , new_name )
f = UploadedFile.find(id)
if f.update_attributes({ f.file.original_filename: new_name }) # this is WRONG, what is right???
new_path = File.join( File.dirname( f.file.current_path ) , new_name ))
FileUtils.mv( f.file.current_path , new_path )
end
return f
end
让我添加它是在它已经上传之后.
Let me add this is after it has been uploaded already.
推荐答案
尽管可以肯定有更优雅的方法,但我仍然可以完成以下工作.感谢您对以下内容的任何评论
I was able to get the following working, although I'm sure there is a more elegant way. I'd appreciate any comments on the following
*将其添加到app/uploaders/file_uploader.rb
*add this to app/uploaders/file_uploader.rb
def rename(new_name)
sf = model.file.file
new_path = File.join( File.dirname( sf.file ) , "#{new_name}#{File.extname( sf.file )}")
new_sf = CarrierWave::SanitizedFile.new sf.move_to(new_path)
model.file.cache!(new_sf)
model.save!
return model
end
谢谢!
这篇关于如何更新/重命名载波上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文