
Follow the steps
Go to terminal and type
rails upload
then
cd upload
mkdir upload\public\data
ruby script/generate model DataFile
Then in data_file.rb
class DataFile < ActiveRecord::Base
def self.save(upload)
name = upload['datafile'].original_filename
directory = "public/data"
# create the file path
path = File.join(directory, name)
# write the file
File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
end
end
Then again in terminal
ruby script/generate controller Upload
class UploadController < ApplicationController
def index
render :file => 'app\views\upload\uploadfile.rhtml'
end
def uploadFile
post = DataFile.save(params[:upload])
render :text => "File has been uploaded successfully"
end
end
uploadFile.rhtml
File Upload
<%= start_form_tag ({:action => 'uploadFile'},
:multipart => true) %>:
<%= file_field 'upload', 'datafile' %>
<%= submit_tag "Upload" %>
<%= end_form_tag %>
Reference link:http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm