2010-02-21 3 views
6

클립을 사용하여 모델에 파일을 추가합니다.Paperclip 및 xhr.sendAsBinary

firefox 3.6의 새로운 기능인 xhr.sendAsBinary을 사용하여 ajax 요청과 함께 파일을 보내고 싶습니다.

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

namesize가 문제없이 내 모델에 저장되지만 파일 자체가 클립에 의해 사로 잡았되지 않은 : 여기

는 내 요청을 구축하는 방법이다.

내 모델

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

마이그레이션

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 

내 컨트롤러 방법이 문제를 해결하는

# 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 

어떤 생각?

감사합니다.

+0

로 파일을 처리하는이

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

처럼 보인다? –

+0

여기 있습니다, 고마워요. – denisjacquemin

답변

4

마침내 제작되었습니다. 파일을 전송

내 자바 스크립트는 이제 종이 클립은 무엇 컨트롤러에 대한 일반 input file

관련 문제