2

我正在尝试使用 as3 将图像发送到服务器。我需要返回托管图像的 url,以便我可以将其分享到 facebook 和 twitter。有人告诉我用 ruby​​ 代码复制我在这里拥有的东西,但不能完全做到。这就是我所拥有的。

红宝石代码:

 require 'rest_client' # gem: rest-client
 require 'json'

 mcz_url = 'server URL'

 mcz_response = nil

 File.open(ARGV[0], 'r') do |f|
 params = { :file => f, :type => 'image', :sizes => nil }

   RestClient.post(mcz_url, params) do |response, request, result|
   mcz_response = response
  end
end

 unless mcz_response.nil?
   json = JSON.parse(mcz_response)
   puts "CDN Url: #{json['original']}"
else
  puts 'MCZ response is null'
end

动作脚本 3

  share_pic.addEventListener(MouseEvent.CLICK, publish); //buton to share a pic

  function publish(evt:MouseEvent):void {
var jpgSource:BitmapData = new BitmapData (stage.width, 256);
    jpgSource.draw(stage);
var jpgEncoder:JPGEncoder = new JPGEncoder(90);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource); //image
trace("sharing pic...");
var request = new URLRequest("Server URL");

    request.method = URLRequestMethod.POST;  
var messages:Array = new Array ();
    messages.push ({field_name:"file:", value: jpgSource});
    messages.push ({field_name:"type", value:"'image'"});
    messages.push ({field_name:"sizes", value:"nil"});

var vars: URLVariables = new URLVariables();
vars.data = JSON.stringify(messages);
request.data = vars;
trace("vars= "+vars);
trace ("request= "+request);
trace("done");


var linkGenerated:String = "";//image string i would like to get from the JSON
    var linkFace:String = "http://www.facebook.com/sharer.php?u="
var linkShare:String = linkFace + linkGenerated ;
var requestFace = new URLRequest(linkShare);
navigateToURL(requestFace,"_blank");

}

非常感谢你的帮助。

4

1 回答 1

0

您可能想要使用URLLoader.load(),并且可能会注意到 FileReference.upload() 以备将来需要。在这两种情况下,您都需要使用带有处理程序的事件侦听器拆分发布功能,一旦服务器完成其进程,这些处理程序将对结果进行操作......

public function publish():void
{
    var urlToPost:URLRequest = new URLRequest("some_url_here");
    //attach any post data, etc.
    var urlLoader:URLLoader = new URLLoader();
    //attach your event listeners, which will be called at some point in the future (asynchronous)
    urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);
    //call load to start the upload
    urlLoader.load( urlToPost );
}

protected function onLoadComplete(e:Event):void
{
    var urlLoader:URLLoader = URLLoader(event.target);
    var linkGenerated:String = urlLoader.data.some_image_string;//image string i would like to get from the JSON
    var linkFace:String = "http://www.facebook.com/sharer.php?u="
    var linkShare:String = linkFace + linkGenerated ;
    var requestFace = new URLRequest(linkShare);
    navigateToURL(requestFace,"_blank");
}

可以在此处找到与您要查找的内容类似的问题/答案。

使用 FileReference.upload() 时,请务必为 DataEvent.UPLOAD_COMPLETE_DATA 附加一个事件侦听器,以便您可以在上传完成后访问服务器返回的任何数据。

于 2013-07-10T14:27:35.290 回答