2010-02-08 97 views
0

我正在尋找一種方法將我的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, 禮服

回答

0

試着改變你的 「文件管理器[文件]」 以 「圖片[文件]」。我認爲這與將其映射到控制器有關。

退房RestfulX,他們很好地解決了這個問題。具體來說,看看他們的XMLHTTPServiceProvider#uploadFile方法。他們在那裏有另一個實現,就像你寫的那樣。我現在使用Paperclip在幾個項目中使用它,並且它完美地工作。

我認爲他們的Pomodo on Rails示例應用程序在github上使用回形針上傳配置文件圖像。

希望有幫助, 蘭斯

相關問題