Flex uses WebService upload photos to implement code _flex

Source: Internet
Author: User
Tags wsdl
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.