paperclip upload zip file

Posted on 2009-12-17
Last Modified: 2013-11-13
Hi, I am trying to upload a zip file, open the file which should contains image files.
My idea was to create an upload button for a file, unzip the file and get image by image, create a Photo which is the class which has has_attached_file.
I am able to upload the file and seems I am able to extract each image file, however I am having problems when creating the Photo which will be added to an event( event has_many photos).
Here is my function where I am trying to create photos.
  def create_photos_from_zip
    logger.debug "create_photos_from_zip -->"
    zip = params[:zip]
    logger.debug "unzipping file #{zip.path} class #{zip.class}" do |entry|
      # entry is a Zip::Entry
      filename = "#{Dir::tmpdir}/#{}_#{}"
      logger.debug "will extract file to #{filename} to"
      p =, 'r+') << Photo.create(:photo=>p)
      #delete the temp file
    res =
    logger.debug "create_photos_from_zip <--"
    return res

This is my create function
 def create
    if (params[:zip].present?)
      res = create_photos_from_zip
      render :nothing=>true
      @photo =[:photo])
      res =
      respond_to do |format|
        if res
          flash[:notice] = 'Photo was successfully created.'
          format.html { redirect_to([@event,@photo]) }
          format.xml  { render :xml => @photo, :status => :created, :location => @photo }
          format.html { render :action => "new" }
          format.xml  { render :xml => @photo.errors, :status => :unprocessable_entity }
* I know I need to work more here, but I am stuck !!!

I am not getting any error, however the files are not attached to the event as well, I am not sure if I am unzipping at the client or server, I am developing in the same machine ...
Any Help would be very appreciated!
Please let me know If you need any other information.
Question by:lusfernandos
    1 Comment
    LVL 2

    Accepted Solution

    Today I redid the function and now it is working, I have a validation for title, which I was not including and I had some confusion where I was extracting the files. so here is the unzip funtion.
      def create_photos_from_zip
        logger.debug "create_photos_from_zip -->"
        zip = params[:zip]
        logger.debug "unzipping file #{zip.path} class #{zip.class}"

        dir = File.join(RAILS_ROOT,"public","events","temp",
        FileUtils.mkdir_p(dir) do |entry|
          # entry is a Zip::Entry
          filename = File.join(dir,
          logger.debug "will extract file to #{filename} to"
          p =
          photo = Photo.create(:photo=>p)
          photo.title = "nova imagem"
 << photo
        #delete zip file
        logger.debug "closing/deleting zip file #{zip.path}"
        logger.debug "saving the event to database"
        logger.debug "create_photos_from_zip <--"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Article by: narshlob
    If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
    In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below:…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now