2010-02-21 12 views
6

Używam spinacza, aby dodać plik do mojego modelu.Paperclip i xhr.sendAsBinary

Chcę użyć nowej funkcji firefox 3.6, xhr.sendAsBinary, aby wysłać plik z prośbą o ajax.

Oto jak buduję moją prośbę:

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 i size są zapisywane w moim modelu bez problemu, ale sam plik nie jest catched przez spinacza.

mój model

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

migracja

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 

i mój kontroler

# 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 

Każdy pomysł jak rozwiązać ten problem?

Dzięki

+0

Co o kontrolerze? –

+0

oto jest, dzięki – denisjacquemin

Odpowiedz

4

W końcu udało się!

mój javascript wysyłania pliku wygląda tak

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); 
    } 
} 

teraz Spinacz obsługuje plik jako normalny input file

Powiązane problemy