File upload in ruby on rails.
There are lots of plugins available for upload files in ruby on rails.
- file_column plugin:
- attachment_fu
- flex_image
- upload_column
- and many more
Accoding to me, all these file are more useful for image uplaoding not other files like voice file, pdf and other.
So I made a defination which check following things and then upload a file on defined location.
1. File extension.
2. Mime type (require mime type plugin)
3. Size of the file
#Define basic things in enviornment/development.rb file
FILE_EXTENSIONS = [".wav",".mp3",".gsm",".pdf"] #Allowed file types
FILE_TEMP_PATH="#{RAILS_ROOT}/public/temp/" #Where file is initialy uploaded
FILE_MIME_EXTENSIONS =["audio/x-wav","audio/mpeg","audio/gsm","audio/x-gsm","application/pdf"] #Allowed file types
FILE_ROOT_PATH= "#{RAILS_ROOT}/public/saved/" #Where file is uploaded permanently
FILE_MAXIMUM_SIZE_FOR_FILE=1048576 #Maximum Size (1MB) define in bytes
#In model
before_save :save_file
attr_accessor :file_data
def get_filename
#define your rename file method
t = Time.now
"file_#{t.strftime("%Y%m%d%H%M")}"
end
def save_file
begin
# No update necessary
return true if self.file_data.blank?
filename = get_filename
return false if filename.nil?
extension = File.extname(self.file_data.original_filename)
if extension.nil?
errors.add(:file_data,"Wrong Extension")
return false
end
# Check if the FILE_TEMP_PATH is created, if not try to create
if !File.directory?(FILE_TEMP_PATH)
File.makedirs(FILE_TEMP_PATH)
end
temp_source = File.join(FILE_TEMP_PATH, filename)
temp_source << extension
final_source = File.join(FILE_ROOT_PATH, filename)
final_source << extension
File.open(temp_source, "wb") { |f| f.write(self.file_data.read)}
size=File.size(temp_source)
extension = File.extname(temp_source)
mime_extension=File.mime_type?(temp_source)
if FILE_MIME_EXTENSIONS.include?(mime_extension) == false
logger.error("Trying to upload file with mime-type: #{mime_extension} ")
errors.add(:file_data,"Only wav, mp3,gsm and pdf files are allowed")
return false
end
if FILE_EXTENSIONS.include?(extension) == false
logger.error("Trying to upload file with extension: #{extension} ")
errors.add(:file_data,"File extension should be wav, mp3, gsm or pdf")
return false
end
if size > FILE_MAXIMUM_SIZE_FOR_FILE
logger.error("Trying to upload file with size: #{size} ")
errors.add(:file_data,"File should not be more than #{FILE_MAXIMUM_SIZE_FOR_FILE} bytes")
return false
end
if valid? #Upload in permanent folder
logger.error("#{self.errors.to_xml}")
self.file=filename
FileUtils.copy_file(temp_source,final_source)
FileUtils.rm(temp_source) if File.exists?(temp_source)
return true
else
errors.add(:file_data,"Invalid file format")
FileUtils.rm(temp_source) #Remove temp file
return false
end
rescue => e
logger.error(e)
errors.add(:file_data,"Invalid file format")
return false
end
end
# In View form
<% form_forbj_name,:html=>{:multipart=>true} do |f| %> <%= f.error_messages %> File: <%=f.file_field :file_data%> <%= f.submit "Create" %> <% end %>
Let me know if you are facing any problem in this..

















This post is really cool. I already fixed my problem in this post. Keep up posting dudez..I will keep bookmark your post so that I have an updates.
Mp3 Rename
File Uploading using RoR on Windows 7 http://madhukaudantha.blogspot.com/2010/11/file-uploading-using-ror-on-windows-7.html