flex4.5 + .net4.0 通过FluorineFx的AMF3协议 以二进制方式图片上传和显示

flex端:

RemotingConnection.as

package com.esri.viewer.utils
{
	import flash.net.NetConnection;  
	import flash.net.ObjectEncoding;  
	import flash.net.URLLoader;  
	import flash.net.URLRequest;  
	import flash.events.Event;  
	import flash.system.Security;  
	
	public class RemotingConnection extends NetConnection  
	{  
		public function RemotingConnection(gatewayUrl:String)  
		{  
			//设置通信权限  
			Security.allowDomain(gatewayUrl);  
			//设置数据格式  
			this.objectEncoding = ObjectEncoding.AMF3;  
			//连接网关  
			this.connect(gatewayUrl);  
		}  
	}  
}


ImageDemo.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
	<fx:Script>
		<![CDATA[
			
			import com.esri.viewer.utils.RemotingConnection;
			
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			import mx.graphics.codec.PNGEncoder;
			
			[Bindable]
			private var _strName:String;
			
			private var conn:RemotingConnection;
			
			private var byte:ByteArray;
			private var fname:String;
			
			private var __url:String = "http://localhost/FxNETChannel_1/Gateway.aspx";
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				// TODO Auto-generated method stub
				var date:Date = new Date();
				fname = date.fullYear.toString() + (date.month+1).toString()+date.date.toString() +date.hours.toString()+date.seconds.toString()+date.minutes.toString()+".png";
				
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
				loader.load(new URLRequest("assets/s.jpg"));
			}
			
			private function loaderCompleteHandler(event:Event):void
			{				
				imgSend.source = event.currentTarget.content.bitmapData;				
				byte = new mx.graphics.codec.PNGEncoder().encode(event.currentTarget.content.bitmapData);
			}
			
			
			private function onResult(result:ByteArray):void
			{
				if(result!=null)
				{					
					var l:Loader = new Loader();
					l.contentLoaderInfo.addEventListener(Event.COMPLETE,lCompleteHandler);
					l.loadBytes(result);
				}
			}
			
			
			private function lCompleteHandler(e:Event):void
			{
				imgGet.source = e.currentTarget.content.bitmapData;
			}
			
			private function onFault(fault:Object):void
			{
				Alert.show("失败!");
			}
			
			private function btnOkClickHandler():void
			{				
				conn = new RemotingConnection(__url);
				conn.call("RemotingService.ImageUploadManage.UploadImage",new Responder(onResult,onFault),byte,fname);				
			}
			
		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<s:layout>
		<s:HorizontalLayout />		
	</s:layout>
	<s:VGroup>
		<s:Button id="btnOk" label="send" click="btnOkClickHandler()"/>
		<s:Scroller>
			<s:Group width="500" height="300">
				<s:Image id="imgSend" source=""/>
			</s:Group>
		</s:Scroller>
	</s:VGroup>
	<s:Scroller>
		<s:Group width="500" height="300">
			<s:Image id="imgGet" source=""/>
		</s:Group>
	</s:Scroller>	
</s:Application>

.net端:

ImageUploadManage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App_CORE.DBAPI;
using FluorineFx.AMF3;
using System.IO;
using System.Web;
using System.Drawing.Imaging;
using System.Drawing;


namespace RemotingService
{
    /// <summary>
    /// 图片上传管理
    /// author:chenxiangji
    /// </summary>
    
    [FluorineFx.RemotingService("Fluorine service:ImageUploadManage")]
    public class ImageUploadManage
    {
        public ImageUploadManage()
        {
        }       

        public ByteArray UploadImage(ByteArray ba,string fileName)
        {
            MemoryStream stream = new MemoryStream(ba.GetBuffer());
            Image img = Bitmap.FromStream(stream);
            Bitmap bitmap = new Bitmap(img);
            MemoryStream tempStream = new MemoryStream();
            bitmap.Save(tempStream,ImageFormat.Png);
            string path = HttpContext.Current.Server.MapPath("FileUpload/Image/" + fileName);
            FileStream fileStream = new FileStream(path, FileMode.Create);
            tempStream.WriteTo(fileStream);
            fileStream.Close();

            ByteArray result = new ByteArray(tempStream);
            return result;
        }
    }
}


需要说明一下就是FluorineFx一定要用AMF3协议,如果用AMF0的话就会出错。



你可能感兴趣的:(.net,function,String,Flex,application,import)