Flex loads file data streams

Source: Internet
Author: User

Flex loads file data streams

This article uses urlstream, URLRequest, and loader to load data streams. files can be images and SWF.

I. Flex code

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute"> <br/> <mx: SCRIPT> <br/> <! -- [CDATA [<br/> Import MX. controls. alert; <br/> Import MX. RPC. soap. loadevent; </P> <p> private var serverurl: String = "http: // localhost: 4051/website/downloadstreamfile. aspx "; <br/> private var stream: urlstream; <br/> private var Loader: loader; <br/> private function loadpicture (Event: mouseevent ): void <br/>{< br/> var URLRequest: URLRequest = new URLRequest (); <br/> stream = new urlstream (); </P> <p> URLRequest. u RL = serverurl + "? File00001.jpg "; <br/> stream. addeventlistener (event. complete, onurlrequesthandler); <br/> stream. load (URLRequest); <br/>}< br/> private function onurlrequesthandler (Event: Event): void <br/>{< br/> If (stream. connected) <br/>{< br/> var B: int; <br/> var Bytes: bytearray = new bytearray (); <br/> try <br/> {<br/> DO <br/> {<br/> B = stream. readbyte (); <br/> bytes. writebyte (B); <br/>}< br/> while (true); <br/>}< br/> catc H (E: eoferror) <br/>{}< br/>}</P> <p> // create the loader mask layer <br/> // var rect: shape = new shape (); <br/> // rect. graphics. beginfill (0 xffffff); <br/> // rect. graphics. drawrect (0, 0, 1000,100 0); <br/> // rect. graphics. endfill (); <br/> loader = new loader (); <br/> loader. addeventlistener (event. complete, onloaderhandler) <br/> loader. loadbytes (bytes); <br/> // loader. mask = rect; <br/> loader. alpha = 1; <br/> loader. cacheasbi TMAP = true; <br/> loader. rotation = 0; <br/> // the size of the loaded image cannot be obtained, the loader must be scaled to a certain level when the image size is predicted. <br/> loader. scalex= 0.6; <br/> loader. scaley = 0.6 <br/> // loader object can only have one sub-Display object and can only be loaded to an image or swfloader <br/> // image. addchild (loader); <br/> myswfloader. addchild (loader); <br/>}< br/> private function onloaderhandler (Event: Event): void <br/>{< br/> // loader. loadbytes () event. complete does not work ??!! <Br/> alert. show (loader. width. tostring (); <br/>}</P> <p> private function setsource (Event: mouseevent): void <br/>{< br/> var STR: string = "http: // localhost: 2286/GetFile. aspx? Pathpolicdydz200907&file=4.swf "; <br/> myswfloader. source = STR; <br/>}< br/>] --> <br/> </MX: SCRIPT> <br/> <mx: image x = "61" Y = "105" id = "image" width = "199" Height = "142"/> <br/> <mx: button x = "61" Y = "45" label = "load image" Click = "loadpicture (event)"/> <br/> <mx: button x = "436" Y = "45" label = "load to swfloader" Click = "setsource (event)"/> <br/> <mx: canvas x = "388" Y = "105"> <br/> <mx: swfloader id = "myswfloader" scalecontent = "true"/> <br/> </MX: canvas> <br/> </MX: Application> <br/>

Ii. file data flow code generated by httpservice

 Protected void page_load (Object sender, eventargs e) <br/>{< br/> string file = request ["file"]; <br/> If (string. isnullorempty (File) <br/> return; <br/> string filepath = server. mappath ("~ /Download/"+ file); <br/> If (file. exists (filepath) <br/> downloadstreamfile (filepath); <br/> response. end (); <br/>}</P> <p> // <summary> <br/> // download the file <br/> /// </Summary> <br />/// <Param name = "serverfilpath"> </param> <br/> /// <Param name = "FILENAME"> </param> <br/> public static void todownload (string serverfilpath, string filename) <br/>{< br/> filestream = new filestream (serverfilpath, filemode. open); <br/> long filesize = filestream. length; <br/> httpcontext. current. response. contenttype = "application/octet-stream"; <br/> httpcontext. current. response. addheader ("content-disposition", "attachment; filename = \" "+ utf_filename (filename) + "\";"); <br/> /// attachment --- download as an attachment <br/> /// inline --- Open Online <br/> httpcontext. current. response. addheader ("Content-Length", filesize. tostring (); <br/> byte [] filebuffer = new byte [filesize]; <br/> filestream. read (filebuffer, 0, (INT) filesize); <br/> httpcontext. current. response. binarywrite (filebuffer); <br/> filestream. close (); <br/> httpcontext. current. response. end (); <br/>}</P> <p> // <summary> <br/> // open the file <br/> /// </Summary> <br />/// <Param name = "serverfilpath"> </param> <br/> /// <Param name = "FILENAME"> </param> <br/> public static void toopen (string serverfilpath, string filename) <br/>{< br/> filestream = new filestream (serverfilpath, filemode. open); <br/> long filesize = filestream. length; <br/> httpcontext. current. response. contenttype = "application/octet-stream"; <br/> httpcontext. current. response. addheader ("content-disposition", "inline; filename = \" "+ utf_filename (filename) +" \ ";"); <br/> httpcontext. current. response. addheader ("Content-Length", filesize. tostring (); <br/> byte [] filebuffer = new byte [filesize]; <br/> filestream. read (filebuffer, 0, (INT) filesize); <br/> httpcontext. current. response. binarywrite (filebuffer); <br/> filestream. close (); <br/> httpcontext. current. response. end (); <br/>}</P> <p> Private Static string utf_filename (string filename) <br/>{< br/> return httputility. urlencode (filename, system. text. encoding. utf8 ); <br/>}</P> <p> // <summary> <br/> // download the file data stream <br/> /// </Summary> <br/> // <Param name = "serverfilpath"> </param> <br/> Public static void downloadstreamfile (string serverfilpath) <br/>{< br/> int blocksize = 1024*100; <br/> byte [] buffer = new byte [blocksize]; <br/> byte [] fileheader = new byte [101]; </P> <p> int startindex = 0; <br/> int bytesread = 0; <br/> int bytesfinish = 0; <br/> int filesize = 0; <br/> string headertext = string. empty; <br/> binaryreader breader = NULL; </P> <p> breader = new binaryreader (file. openread (serverfilpath); <br/> filesize = convert. toint32 (breader. basestream. length); </P> <p> while (bytesfinish <filesize) <br/> {<br/> If (bytesfinish + blocksize <filesize) <br/> bytesread = breader. read (buffer, startindex, blocksize); <br/> else <br/> bytesread = breader. read (buffer, startindex, filesize-bytesfinish); </P> <p> bytesfinish + = bytesread; <br/> httpcontext. current. response. outputstream. write (buffer, 0, bytesread); <br/>}< br/> breader. close (); <br/> httpcontext. current. response. end (); <br/>}

Note: The size of images and other files cannot be obtained during loading in this example, and the display is inaccurate.

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.