[英]Uploading images from Flex to Rails using Paperclip
我正在尋找一種將在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設置為字節數組,則會收到錯誤消息,即上傳為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
有誰知道如何解決這個問題?
禮服
嘗試將“ filemanager [文件]”更改為“圖片[文件]”。 我認為這與將其映射到控制器有關。
簽出RestfulX ,他們很好地解決了這個問題。 具體來說,請查看其XMLHTTPServiceProvider#uploadFile
方法。 他們在其中有另一個實現,就像您編寫的那樣。 我現在在幾個帶有Paperclip的項目中使用它,並且效果很好。
我認為他們在github 上的Pomodo on Rails示例應用程序使用回形針上傳個人資料圖像。
希望有幫助,蘭斯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.