javascript - Paperclip 和 xhr.sendAsBinary

标签 javascript ruby-on-rails ajax firefox paperclip

我使用回形针将文件添加到我的模型。

我想使用 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

和我的 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

知道如何解决这个问题吗?

谢谢

最佳答案

我终于成功了!

我的 javascript 发送文件看起来像这样

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

现在 Paperclip 将文件作为普通输入文件

处理

关于javascript - Paperclip 和 xhr.sendAsBinary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2306910/

相关文章:

javascript - 无法加载资源: the server responded with a status of 404 (Not Found) javascript/application. js

javascript - EnumDropDownListFor 更改 ajax 调用操作并将所选值分配给数据库的一个字段

javascript - Jquery - 如何为多个元素绑定(bind)单击事件?

javascript - Canvas 并根据网格获取坐标(通过鼠标 x 鼠标 y)

ruby-on-rails - 在 Rails 中,无法为 {"adapter"= >"postgresql"创建数据库,

sql - ActiveRecord has_many 通过多态 has_many

ajax - 如何在单击 p :dataTable through ajax 中的“全选”复选框时调用方法

php - 如何使用 phpunit 测试用例测试 ajax 调用?

javascript - 正则表达式匹配精确路径与两个/

javascript - 我们可以使用 javascript 将图像推送到浏览器缓存吗