WebService End Code
Copy Code code as follows:
<summary>
Uploading files to a remote server
</summary>
<param name= "filebytes" > File Flow </param>
<param name= "filename" > FileName </param>
<returns> string </returns>
[WebMethod (Description = "Upload file to remote server.")]
public string UploadFile (byte[] filebytes, string fileName)
{
Try
{
MemoryStream MemoryStream = new MemoryStream (filebytes); 1. Define and instantiate a memory stream to hold the byte array that is submitted.
FileStream fileupload = new FileStream (Server.MapPath (".") + "\" + FileName, FileMode.Create); 2. Define the actual file object and save the uploaded file.
Memorystream.writeto (FileUpload); 3. Write the data in the memory stream to the physical file
Memorystream.close ();
Fileupload.close ();
FileUpload = null;
MemoryStream = null;
Return "file has been uploaded successfully";
}
catch (Exception ex)
{
Return ex. message;
}
}
Flex Client Code
Copy Code code as follows:
<?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= "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>
<!--place non-visual elements (such as services, value objects) here-->
<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= "width=" "583" "height=" 397 "source=" file:/g:/360 Cloud disk/Photo/2013beijing Mapofsubway.jpg "/>
</s:Application>