flex利用webservice上傳照片實(shí)現(xiàn)代碼
更新時(shí)間:2014年05月02日 10:13:50 作者:
這篇文章主要介紹了flex利用webservice上傳照片實(shí)現(xiàn)代碼,需要的朋友可以參考下
WebService端代碼
/// <summary>
/// 上傳文件到遠(yuǎn)程服務(wù)器
/// </summary>
/// <param name="fileBytes">文件流</param>
/// <param name="fileName">文件名</param>
/// <returns>字符串</returns>
[WebMethod(Description = "上傳文件到遠(yuǎn)程服務(wù)器.")]
public string UploadFile(byte[] fileBytes, string fileName)
{
try
{
MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定義并實(shí)例化一個(gè)內(nèi)存流,以存放提交上來的字節(jié)數(shù)組。
FileStream fileUpload = new FileStream(Server.MapPath(".") + "\\" + fileName, FileMode.Create); ///2.定義實(shí)際文件對(duì)象,保存上載的文件。
memoryStream.WriteTo(fileUpload); ///3.把內(nèi)存流里的數(shù)據(jù)寫入物理文件
memoryStream.Close();
fileUpload.Close();
fileUpload = null;
memoryStream = null;
return "文件已經(jīng)上傳成功";
}
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>
<!-- 將非可視元素(例如服務(wù)、值對(duì)象)放在此處 -->
<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>
復(fù)制代碼 代碼如下:
/// <summary>
/// 上傳文件到遠(yuǎn)程服務(wù)器
/// </summary>
/// <param name="fileBytes">文件流</param>
/// <param name="fileName">文件名</param>
/// <returns>字符串</returns>
[WebMethod(Description = "上傳文件到遠(yuǎn)程服務(wù)器.")]
public string UploadFile(byte[] fileBytes, string fileName)
{
try
{
MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定義并實(shí)例化一個(gè)內(nèi)存流,以存放提交上來的字節(jié)數(shù)組。
FileStream fileUpload = new FileStream(Server.MapPath(".") + "\\" + fileName, FileMode.Create); ///2.定義實(shí)際文件對(duì)象,保存上載的文件。
memoryStream.WriteTo(fileUpload); ///3.把內(nèi)存流里的數(shù)據(jù)寫入物理文件
memoryStream.Close();
fileUpload.Close();
fileUpload = null;
memoryStream = null;
return "文件已經(jīng)上傳成功";
}
catch (Exception ex)
{
return ex.Message;
}
}
Flex客戶端代碼
復(fù)制代碼 代碼如下:
<?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>
<!-- 將非可視元素(例如服務(wù)、值對(duì)象)放在此處 -->
<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>
相關(guān)文章
flex中使用css樣式修改TextArea滾動(dòng)條的皮膚代碼
使用css樣式修改TextArea滾動(dòng)條的皮膚,具體示例代碼如下,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08在as中監(jiān)聽自定義事件并處理事件的實(shí)例代碼
點(diǎn)擊一張圖片,響應(yīng)事件。必須在AS中,去監(jiān)聽事件,并處理事件,下面是具體的實(shí)現(xiàn)思路及功能代碼,感興趣的朋友可以參考下哈2013-06-06flex chrome瀏覽器調(diào)試出現(xiàn)空白的解決方法
flex瀏覽器調(diào)試出現(xiàn)空白,原因是谷歌有個(gè)默認(rèn)的flash播放器,只要將默認(rèn)的播放器禁用,留下新安裝的插件,就OK了2014-02-02flex實(shí)現(xiàn)DataGrid高亮顯示數(shù)據(jù)功能的解決方案
原生的DataGrid根本無法達(dá)到所要的效果,目前一般就是來改寫原生的DataGrid,只需重新寫一個(gè)類來重寫drawRowBackground方法就可以了2013-10-10Flex Label控件豎排顯示文字的實(shí)現(xiàn)代碼
Label控件豎排顯示文字一般都是橫排的,接下來為大家分享個(gè)方法讓它變成豎排,感興趣的童鞋們可以學(xué)習(xí)下,希望對(duì)你有所幫助2013-04-04FLEX ArrayCollection刪除過濾的數(shù)據(jù)問題解決
ArrayCollection添加過濾器后,調(diào)用removeItemAt()是無法刪除的,下面有個(gè)不錯(cuò)的解決方法,大家可以參考下2014-06-06