Flex アプリで作成した画像をレールにアップロードする方法を探しています。ペーパークリップを使用しようとしましたが、うまくいかないようです。
ここにこのチュートリアルがあります: http://blog.alexonrails.net/?p=218
問題は、FileReference を使用してクライアント コンピューター上のファイルを参照していることです。.upload(...) 関数を呼び出し、データをアップロード コントローラーに送信します。しかし、Flex-App で変更された画像をアップロードするために URLLoader を使用しています。まず、チュートリアルのコードは次のとおりです。
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 に設定すると、アップロードが nil であるというエラーが表示されます。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、タックス