我正在寻找一种将在我的 flex 应用程序中创建的图像上传到 rails 的方法。我试过用回形针,但它似乎不起作用。
我在这里有这个教程:http: //blog.alexonrails.net/ ?p=218
问题是,他们使用 FileReference 来浏览客户端计算机上的文件。他们调用 .upload(...) 函数并将数据发送到上传控制器。但我正在使用 URLLoader 上传图像,该图像在我的 Flex-App 中进行了修改。首先,这是教程中的代码:
private function selectHandler(event:Event):void {
var vars:URLVariables = new URLVariables();
var request:URLRequest = new URLRequest(uri);
request.method = URLRequestMethod.POST;
vars.description = "My Description";
request.data = vars;
var uploadDataFieldName:String = 'filemanager[file]';
fileReference.upload(request, uploadDataFieldName);
}
我不知道如何设置
var uploadDataFieldName:String = 'filemanager[file]';
在 URLLoader 中。我已将图像数据压缩为 ByteArray 中的 JPEG。它看起来像这样:
public function savePicture():void {
var filename:String = "blubblub.jpg";
var vars:URLVariables = new URLVariables();
vars.position = layoutView.currentPicPosition;
vars.url = filename;
vars.user_id = 1;
vars.comic_id = 1;
vars.file_content_type = "image/jpeg";
vars.file_file_name = filename;
var rawBytes:ByteArray = new JPGEncoder(75).encode(bitmapdata);
vars.picture = rawBytes;
var request:URLRequest = new URLRequest(Data.SERVER_ADDR + "pictures/upload");
request.method = URLRequestMethod.POST;
request.data = vars;
var loader:URLLoader = new URLLoader(request);
loader.addEventListener(Event.COMPLETE, savePictureHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandlerUpload);
loader.load(request);
}
如果我将 var.picture URLVariable 设置为字节数组,则会收到错误消息,即上传为零。这是 Rails 部分:
图片模型:
需要“回形针”
class Picture < ActiveRecord::Base
# relations from picture
belongs_to :comic
belongs_to :user
has_many :picture_bubbles
has_many :bubbles, :through => :picture_bubbles
# attached file for picture upload -> with paperclip plugin
has_attached_file :file, :path => "public/system/pictures/:basename.:extension"
end
以及带有上传功能的图片控制器:
class PicturesController < ApplicationController
protect_from_forgery :except => :upload
def upload
@picture = Picture.new(params[:picture])
@picture.position = params[:position]
@picture.comic_id = params[:comic_id]
@picture.url = params[:url]
@picture.user_id = params[:user_id]
if @picture.save
render(:nothing => true, :status => 200)
else
render(:nothing => true, :status => 500)
end
end
end
有谁知道如何解决这个问题?
谢谢,晚礼服