読者です 読者をやめる 読者になる 読者になる

画像をZIPでダウンロード

Flex

久しぶりにFlexやってみましたー
バグありありですがてすてす

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark" title="TestApp"
        creationComplete="viewnavigatorapplication1_creationCompleteHandler(event)"
        >
    <fx:Script>
        <![CDATA[
            import flash.net.navigateToURL;
            import flash.sensors.Geolocation;
            import mx.events.FlexEvent;
            import mx.graphics.codec.JPEGEncoder;
            import mx.rpc.events.ResultEvent;
            import spark.components.CheckBox;
            import spark.components.Image;
            import spark.components.VGroup;
            import nochump.util.zip.ZipEntry;
            import nochump.util.zip.ZipOutput;
            public var arrImage:Array = new Array();
            protected function hts_resultHandler(event:ResultEvent):void
            {
                var len:int = hts.lastResult.item.length();
                var item:Object = hts.lastResult.item;
                for ( var i: int = 0 ; i < len ; i ++ ){
                    var image:Image =  new Image();
                    image.width = 100;
                    image.height = 100;
                    image.source = "" + item[i].image.image1;
                    image.name = "img_" + i ;
                    arrImage[i] = image;
                    image.addEventListener(Event.COMPLETE , onComp );
                    var vg:VGroup = new VGroup();
                    var checkbox:CheckBox = new CheckBox();
                    checkbox.label ="chk_" + i;
                    checkbox.name = "chk_" + i;
                    vg.addElement(image);
                    vg.addElement(checkbox);
                    tl.addElement(vg);
                    image.addEventListener(TouchEvent.TOUCH_TAP , onTap );
                    image.addEventListener(MouseEvent.CLICK , onClick);
                }
            }
            protected function onComp( evt:Event):void{
                var fade:Fade = new Fade();
                fade.alphaFrom =0;
                fade.alphaTo = 1;
                fade.duration = 5000;
                fade.play([evt.currentTarget]);
            }
            protected function onTap( evt:TouchEvent ):void{
                trace(evt.target.name );
            }
            protected function onClick( evt:MouseEvent ):void{
                var i:int = String(evt.currentTarget.name ).split("_")[1];
                var checkbox:CheckBox =(tl.getChildAt(i) as VGroup).getChildByName("chk_" + i ) as CheckBox
                if( checkbox.selected ){
                    checkbox.selected = false;
                }else{
                    checkbox.selected = true;
                }
//                navigateToURL( new URLRequest( hts.lastResult.item[i].url ) );
            }
            protected function viewnavigatorapplication1_creationCompleteHandler(event:FlexEvent):void
            {
                if( Geolocation.isSupported ){
                    var geo:Geolocation = new Geolocation();
                    geo.setRequestedUpdateInterval(0);
                    geo.addEventListener( GeolocationEvent.UPDATE , onUpdate );
                }else{
                    trace( "no gps");
                }
                hts.send();
            }
            protected function onUpdate( evt:GeolocationEvent ):void{
                trace( evt.latitude );
            }
            protected function saveBtn_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                // file info
                var fileName:String = "helloworld.bin";
                var fileData:ByteArray = new ByteArray();
                fileData.writeUTF("Hello World!");
                var zipOut:ZipOutput = new ZipOutput();
                // Add entry to zip
                var ze:ZipEntry = new ZipEntry(fileName);
                zipOut.putNextEntry(ze);
                zipOut.write(fileData);
                zipOut.closeEntry();
                // end the zip
                zipOut.finish();
                // access the zip data
                var zipData:ByteArray = zipOut.byteArray;
                //-----[ファイル1]
                var dat:String = "hoge";
                //Windowsの改行に置換
                dat = dat.replace(/\r/g, "\r\n");
                var zipOut:ZipOutput = new ZipOutput();
                var fileData1:ByteArray = new ByteArray();
                fileData1.writeUTFBytes(dat);
                zipOut.putNextEntry(new ZipEntry("userinput.txt"));
                zipOut.write(fileData1);
                zipOut.closeEntry();
                //-----[ファイル2]
                var fileData2:ByteArray = new ByteArray();
                fileData2.writeUTFBytes("ハローワールド!");
                zipOut.putNextEntry(new ZipEntry("helloworld.txt"));
                zipOut.write(fileData2);
                zipOut.closeEntry();
                //-----[ファイル3]
                //jpegファイル作成
//                var bitmapData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
//                bitmapData.draw(this);
//                var jpgEncoder:JPGEncoder = new JPEGEncoder(80);
//                var fileData3:ByteArray = new ByteArray();
    //var image:image;
                var im:Image = arrImage[0];
                var rec:Rectangle = new Rectangle(0,0,im.width,im.height);
                var fileData3:ByteArray = im.bitmapData.getPixels( rec );
                zipOut.putNextEntry(new ZipEntry( arrImage[0].name + ".jpg"));
                zipOut.write(fileData3);
                zipOut.closeEntry();
                zipOut.finish();
                //ファイルリファレンスで保存
                var fr:FileReference = new FileReference();
                fr.save(zipOut.byteArray, "files.zip"); // ダイアログを表示する
                function onComplete(e:Event):void {
                    trace(fr.name);
                }
            }
        ]]>
        </fx:Script>
    <fx:Declarations>
        <s:Fade id="myFade"/>
        <!-- 非ビジュアルエレメント (サービス、値オブジェクトなど) をここに配置 -->
        <s:HTTPService id="hts" url="http://moeten.info/maidcafe/?type=shop&amp;wgps_lat=35.698349&amp;wgps_lon=139.773104&amp;m=api"
                       resultFormat="e4x" result="hts_resultHandler(event)"/>
    </fx:Declarations>
    <s:Scroller width="100%" height="100%">
        <s:TileGroup id="tl"/>
    </s:Scroller>
    <s:Button id="saveBtn" label="save" bottom="10" click="saveBtn_clickHandler(event)"/>
</s:View>