我正在尋找一種方法將我的flex應用程序中創建的圖像上傳到導軌。我試圖使用回形針,但似乎並不奏效。使用回形針將圖像從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到ByteArray的,然後我得到的錯誤,上載爲零。 這裏是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
有誰知道解決這個問題?
THX, 禮服