flex利用webservice上傳照片實現(xiàn)代碼
更新時間:2014年05月02日 10:13:50 作者:
這篇文章主要介紹了flex利用webservice上傳照片實現(xiàn)代碼,需要的朋友可以參考下
WebService端代碼
/// <summary>
/// 上傳文件到遠程服務器
/// </summary>
/// <param name="fileBytes">文件流</param>
/// <param name="fileName">文件名</param>
/// <returns>字符串</returns>
[WebMethod(Description = "上傳文件到遠程服務器.")]
public string UploadFile(byte[] fileBytes, string fileName)
{
try
{
MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定義并實例化一個內存流,以存放提交上來的字節(jié)數組。
FileStream fileUpload = new FileStream(Server.MapPath(".") + "\\" + fileName, FileMode.Create); ///2.定義實際文件對象,保存上載的文件。
memoryStream.WriteTo(fileUpload); ///3.把內存流里的數據寫入物理文件
memoryStream.Close();
fileUpload.Close();
fileUpload = null;
memoryStream = null;
return "文件已經上傳成功";
}
catch (Exception ex)
{
return ex.Message;
}
}
Flex客戶端代碼
<?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 mx.controls.Alert;
import mx.events.FlexEvent;
import mx.graphics.codec.JPEGEncoder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var width :int = imgID.width;
var height :int = imgID.height;
var bitmapData:BitmapData =new BitmapData(width,height);
bitmapData.draw(imgID);
var byteArr:ByteArray = bitmapData.getPixels(new Rectangle(0,0,width,height));
var byteArr123:ByteArray =new JPEGEncoder().encodeByteArray(byteArr,width,height);
webService.UploadFile(byteArr123,"123.png");
}
protected function webService_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString());
}
protected function webService_successHandler(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務、值對象)放在此處 -->
<s:WebService id="webService" wsdl="http://10.19.1.48/upImg/Service1.asmx?WSDL" fault="webService_faultHandler(event)">
<s:operation name="UploadFile" result="webService_successHandler(event)"></s:operation>
</s:WebService>
</fx:Declarations>
<mx:Image id="imgID" x="186" y="103" width="583" height="397" source="file:/G:/360云盤/照片/2013Beijing MapOfSubway.jpg"/>
</s:Application>
復制代碼 代碼如下:
/// <summary>
/// 上傳文件到遠程服務器
/// </summary>
/// <param name="fileBytes">文件流</param>
/// <param name="fileName">文件名</param>
/// <returns>字符串</returns>
[WebMethod(Description = "上傳文件到遠程服務器.")]
public string UploadFile(byte[] fileBytes, string fileName)
{
try
{
MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定義并實例化一個內存流,以存放提交上來的字節(jié)數組。
FileStream fileUpload = new FileStream(Server.MapPath(".") + "\\" + fileName, FileMode.Create); ///2.定義實際文件對象,保存上載的文件。
memoryStream.WriteTo(fileUpload); ///3.把內存流里的數據寫入物理文件
memoryStream.Close();
fileUpload.Close();
fileUpload = null;
memoryStream = null;
return "文件已經上傳成功";
}
catch (Exception ex)
{
return ex.Message;
}
}
Flex客戶端代碼
復制代碼 代碼如下:
<?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 mx.controls.Alert;
import mx.events.FlexEvent;
import mx.graphics.codec.JPEGEncoder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var width :int = imgID.width;
var height :int = imgID.height;
var bitmapData:BitmapData =new BitmapData(width,height);
bitmapData.draw(imgID);
var byteArr:ByteArray = bitmapData.getPixels(new Rectangle(0,0,width,height));
var byteArr123:ByteArray =new JPEGEncoder().encodeByteArray(byteArr,width,height);
webService.UploadFile(byteArr123,"123.png");
}
protected function webService_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString());
}
protected function webService_successHandler(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務、值對象)放在此處 -->
<s:WebService id="webService" wsdl="http://10.19.1.48/upImg/Service1.asmx?WSDL" fault="webService_faultHandler(event)">
<s:operation name="UploadFile" result="webService_successHandler(event)"></s:operation>
</s:WebService>
</fx:Declarations>
<mx:Image id="imgID" x="186" y="103" width="583" height="397" source="file:/G:/360云盤/照片/2013Beijing MapOfSubway.jpg"/>
</s:Application>
相關文章
Flex動態(tài)生成可編輯的DataGrid具體實現(xiàn)代碼
DataGrid具有以下功能:表頭是動態(tài)生成的、每行都是有序號的、每行都是可以編輯、插入、刪除、修改,接下來為大家分享下Flex如何動態(tài)生成可編輯的DataGrid2013-04-04
Flex中如何動態(tài)生成DataGrid以及動態(tài)生成表頭
因某些需要,DataGrid及其表頭需要動態(tài)生成,網上的解決方案打多籠統(tǒng),下面有個不錯的解決方法,感興趣的朋友可以參考下2013-10-10
Flex中TextInput組件設置限制某些字符的輸入的方法
TextInput組件設置限制輸入例如限制某個字符的輸入、設置只能輸入某些字符等等,下面是具體的示例,喜歡的朋友可以參考下2014-01-01
flex的tree動態(tài)加載大量數據與滾動條相關問題探討
本文將對flex的tree動態(tài)加載大量數據與滾動條相關的問題進行探討,感興趣的朋友可以參考下哈,希望對你有所幫助2013-05-05

