Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip and xhr.sendAsBinary

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

like image 382
denisjacquemin Avatar asked Feb 21 '10 18:02

denisjacquemin


People also ask

What is an XHR file?

XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.

Why is it called XHR?

The XMLHttpRequest object, or XHR, is a JavaScript API that allows us to transfer data between a client and a server. It was named at a time when XML was all the rage, but it can be used with any type of data, including JSON, which is the current de facto standard.


1 Answers

I finally made it work!

my javascript sending file looks like this

 send : function() {
     try {
         var xhr = new XMLHttpRequest;
         //var url = this.form.action;
         var url = '/photos';

         var boundary    = this.generateBoundary();
         var contentType = "multipart/form-data; boundary=" + boundary;

         this.filesToUpload.forEach(function(file, index, all) {

             xhr.open("POST", url, true);
             xhr.setRequestHeader("Content-Type", contentType);

             for (var header in this.headers) {
                 xhr.setRequestHeader(header, headers[header]);
             }


             var CRLF  = "\r\n";
             var request = "--" + boundary  + CRLF;

             request += 'Content-Disposition: form-data; ';
             request += 'name="' + 'photo[name]' + '"' + CRLF + CRLF;
             request += file.name + CRLF;

             request += "--" + boundary + CRLF;

             request += 'Content-Disposition: form-data; ';
             request += 'name="' + 'photo[photo]' + '"; ';
             request += 'filename="'+ file.fileName + '"' + CRLF;

             request += "Content-Type: application/octet-stream" + CRLF + CRLF;
             request += file.value + CRLF;
             request+= "--" + boundary + "--" + CRLF;

             xhr.sendAsBinary(request);
         });
         // finally send the request as binary data
         //xhr.sendAsBinary(this.buildMessage(this.filesToUpload, boundary));
     } catch(e) {
         alert('send Error: ' + e);
     }
 }

now Paperclip handles the file as a normal input file

like image 112
denisjacquemin Avatar answered Sep 24 '22 09:09

denisjacquemin