Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

FILE UPLOADING WITH UNIQUE NAME (See related posts)

src_filename = params[:file][:name] 
if !src_filename.original_filename.empty? 
  hashed_name = Digest::SHA1.hexdigest(Time.now.to_s) 
  filename = "#{RAILS_ROOT}/public/attachments/" + hashed_name + "_" + src_filename.original_filename 
  if File.open(filename, 'w'){ |f| f.write(src_filename.read) }  
     attachment = Attachment.new 
     attachment.name = src_filename.original_filename 
     attachment.hashed_name = hashed_name 
     attachment.size = src_filename.size 
     comment.attachments << attachment 
  end  
end 



You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts