2010-02-08 95 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上使用回形针上传配置文件图像。

希望有帮助, 兰斯

相关问题