Paperclip and xhr.sendAsBinary

Posted by Denis on Stack Overflow See other posts from Stack Overflow or by Denis
Published on 2010-02-21T18:30:19Z Indexed on 2010/03/14 8:45 UTC
Read the original article Hit count: 642

Hi,

I use paperclip to add a file to my model.

I want to use the new feature of firefox 3.6, xhr.sendAsBinary, to send a file with an ajax request.

Here is how I build my request :

var xhr = new XMLHttpRequest();


xhr.open("POST", "/photos?authenticity_token=" + token 
                        + "&photo[name]=" + img.name
                        + "&photo[size]=" + img.size);

xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
xhr.sendAsBinary(bin);

name and size are saved in my model without problem but the file itself is not catched by paperclip.

my model

class Photo < ActiveRecord::Base
  has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end

the migration

def self.up
  add_column :photos, :photo_file_name,     :string
  add_column :photos, :photo_content_type,  :string
  add_column :photos, :photo_file_size,     :integer
  add_column :photos, :photo_updated_at,    :datetime
end

and my controller

  # POST /photos
  # POST /photos.xml
  def create
    @photo = Photo.new(params[:photo])

    respond_to do |format|
      if @photo.save
        format.html { redirect_to(@photo, :notice => 'Photo was successfully created.') }
        format.xml  { render :xml => @photo, :status => :created, :location => @photo }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @photo.errors, :status => :unprocessable_entity }
      end
    end
  end

Any idea how to solve this issue?

Thanks

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about JavaScript