flex4.5 + .net4.0 以二进制方式上传图片

时间:2022-08-29 17:47:14
 

 

flex端: 

 

 1<?xml version="1.0" encoding="utf-8"?>
 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
 3                xmlns:s="library://ns.adobe.com/flex/spark" 
 4                xmlns:mx="library://ns.adobe.com/flex/mx" 
 5                minWidth="955" minHeight="600"                
 6                xmlns:esri="http://www.esri.com/2008/ags">
 7     <fx:Script>
 8         <![CDATA[
 9             import mx.events.FlexEvent;
10             import mx.graphics.ImageSnapshot;
11             import mx.graphics.codec.PNGEncoder;
12             
13             
14             private var __url:String = "http://localhost/FxNETChannel_1/UploadImageData.aspx?";
15             private var bitmapData:BitmapData;
16             private var urlRequest:URLRequest;
17             private var urlLoader:URLLoader;
18             
19             protected function btnSendMap_clickHandler(event:MouseEvent):void
20             {
21                 // TODO Auto-generated method stub
22                 
23                 //获得页面地图的BitmapData对象
24                 bitmapData = ImageSnapshot.captureBitmapData(map);
25                 
26                 //创建URLRequest对象
27                 urlRequest = new URLRequest();
28                 
29                 //设置请求地址
30                 urlRequest.url = __url;
31                 
32                 //设置请求方式
33                 urlRequest.method = URLRequestMethod.POST;
34                 
35                 //把BitmapData 转换成 ByteArray
36                 var byte:ByteArray = new PNGEncoder().encode(bitmapData);
37                 
38                 //设置请求数据
39                 urlRequest.data = byte;
40                 
41                 //设置请求内容类型
42                 urlRequest.contentType = "image/png";
43                 
44                 //创建URLLoader对象
45                 urlLoader = new URLLoader();
46                 
47                 //设置URLLoader的格式为二进制
48                 urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
49                 
50                 //加载
51                 urlLoader.load(urlRequest);
52             }
53             
54         ]]>
55     </fx:Script>
56     <fx:Declarations>
57         <!-- Place non-visual elements (e.g., services, value objects) here -->
58     </fx:Declarations>
59     
60     <s:HGroup width="100%" height="100%">
61         <esri:Map id="map">
62             <esri:ArcGISTiledMapServiceLayer width="100%" height="100%" url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
63         </esri:Map>
64         <s:Button id="btnSendMap" label="发送地图" click="btnSendMap_clickHandler(event)"/>
65     </s:HGroup>
66 </s:Application>

 

.net端:

 UploadImageData.aspx文件

 1  using System;
 2  using System.Data;
 3  using System.Configuration;
 4  using System.Collections;
 5  using System.Web;
 6  using System.Web.Security;
 7  using System.Web.UI;
 8  using System.Web.UI.WebControls;
 9  using System.Web.UI.WebControls.WebParts;
10  using System.Web.UI.HtmlControls;
11  using System.IO;
12  using System.Data.OracleClient;
13  using App_CORE.DBAPI;
14  using RemotingService;
15  using System.Drawing;
16  using System.Drawing.Imaging;
17 
18  public  partial  class UploadImageData : System.Web.UI.Page
19 {
20      public  void Page_Load( object sender, EventArgs e)
21     {
22         Stream _stream = Request.InputStream;        
23          byte[] Bytes =  new  byte[_stream.Length];
24          string path = System.Web.HttpContext.Current.Request.MapPath( " FileUpload/Image/ ");
25         FolderCreate(path); 
26          try
27         {
28             MemoryStream stream =  new MemoryStream();
29             stream.Write(Bytes,  0, Bytes.Length);
30             Bitmap bitMap =  new Bitmap(_stream);
31             HttpContext.Current.Response.ContentType =  " image/png ";
32              string imgName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
33             bitMap.Save(path + imgName +  " .png ", ImageFormat.Png);
34             HttpContext.Current.Response.Clear();
35             HttpContext.Current.ApplicationInstance.CompleteRequest();
36         }
37          catch (Exception ex)
38         {
39             System.Web.HttpContext.Current.Response.Write(ex.Message);
40         }
41     }
42 
43      ///   <summary>
44       ///  判断文件夹是否存在,如果不存在则新建一个
45       ///   </summary>
46       ///   <param name="path"></param>
47       public  static  void FolderCreate( string path)
48     {
49          if (!Directory.Exists(path))
50         {
51             Directory.CreateDirectory(path);
52         }
53     }

54 }