rss

Jun
16

File Upload Helpers

Posted (Amit) in Ruby on Rails on 16-06-2008

Handy little helpers to check if a file was provided to a controller, and to save it.

1
2
3
4
5
6
7
8
9
10
11
 def file_provided?
   [StringIO, Tempfile].include?(@file.class) and @file.size.nonzero?
 end 
 
 def save_file
   if @file.is_a? Tempfile
     FileUtils.cp(@file.path, path)
   elsif @file.is_a? StringIO
     File.open(path, "wb") { |disk_file| disk_file << @file.read }
   end
 end
  • Share/Bookmark

Post a comment



Amit Yadav is Digg proof thanks to caching by WP Super Cache