Hi guys,
Its been a long time since I posted any article. This time I come up with Interesting Tutorial which is Bulk Upload. Normally in most of the sites we have seen that there is a file upload field where we can upload a zip file and it will extract automatically.
Now the Point is How it can be done is Ruby on Rails? The answer is very simple, all we need are two gems + few lines of code. Wondering How?? See this in action…
Install two gems first:
for Linux Users:
1. sudo gem install rubyzip
2. sudo gem install fastercsv
for windows users: remove sudo and install above gems.
now open up your rhtml page and write down the following code:
<% form_for :file_upload,:url => {:controller=>'bulkupload',:action=>'upload_file'}, :html => { :multipart => true, :target => "frame", :id => "file_upload" } do |f| %>
<input type="file" name="file_upload_product[file_name]"/>
<input type="submit" value="Upload"/>
<%end%>
Now Open up your bulkupload controller and create method upload_file in that
require 'faster_csv'
require 'fileutils'
class BulkuploadProductController < ApplicationController
def bulk_upload
begin
@file=FileUpload.new
@file.file_name=params[:file_upload_product]['file_name']
if @file.save
responds_to_parent do
render :update do |page|
page << "$('file_uploaded_id').value="+@file.id.to_s
page.replace_html 'upload_message',"<span class='heading4'>File has been moved to server. Please click submit to upload.</span>"
end
end
else
responds_to_parent do
render :update do |page|
page.replace_html 'upload_message',"<span class='table_commands_row'>Please Upload File</span>"
end
end
end
rescue Exception=>e
puts "ERROR :: bulkupload_products :: upload_file :: #{e.to_s}"
responds_to_parent do
render :update do |page|
page.replace_html 'upload_message',"<span class='table_commands_row'>Some internal Error has occurred</span>"
end
end
end
end
end
Let me know if you have any doubts

Leave a comment