Bulk Upload with Rubyzip

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"/>

Now Open up your bulkupload controller and create method upload_file in that

require 'faster_csv'
require 'fileutils'
class BulkuploadProductController < ApplicationController
  def bulk_upload
     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>"
      responds_to_parent do
       render :update do |page|
        page.replace_html 'upload_message',"<span class='table_commands_row'>Please Upload File</span>"
     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>"

Let me know if you have any doubts

2 thoughts on “Bulk Upload with Rubyzip

  1. Joris August 4, 2009 / 7:38 PM

    Hi, I cannot follow your tutorial. It looks like some things are missing.

    The code provided is a normal file upload. How is the zip file extracted and how to use the extracted files? And why is ‘faster_csv’ needed?

  2. Puneet Pandey August 12, 2009 / 11:28 AM

    Hi Joris,

    I looked into it.. all the points you have mentioned are true, basically it is for CSV files. Upload your CSV file and it will upload the columns into your table..

    I have the article for rubyzip as well, will post it soon..

    thanks for pointing out

    Puneet Pandey

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s