Not yet rated
Tags:



Problem

File promises is a new API (URLFilePromise) that allows you to access resources at a certain URL and drag them out of the AIR application as a file promise into the local machine. There is a need of a simple but effective example of how to use this API

Solution

Application below allows dragging of items from a list into the user’s local machine and then the copying of the files.

Detailed explanation

File promises is a new API (URLFilePromise) that allows you to access resources at a certain URL and drag them out of the AIR application as a file promise  into the local machine.  File promise, as the name suggests is a commitment that the file exists without actually checking. Once the files are dropped the request will be made to download the file to your local machine.


The URLFilePromise class implements the contract of IFilePromise using URLStream and URLRequest objects as the data source.

To create a URL file promise we create a URLFilePromise object, add the object to an array, and then start the drag passing the Clipboard object the array of file promises. See below.


var items:Array = fileData.selectedItems;
var promises:Array = new Array();
               
for each (var item:Object in items)
{
     var filePromise:URLFilePromise = new URLFilePromise();
     var request:URLRequest = new URLRequest(item.url);
                   
      filePromise.request = request;
      filePromise.relativePath = item.name;
      promises.push(filePromise);
 }

Take a look at the application below, which allows the dragging of items from a list into the user's local machine and then the copying of the files.

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark"
                      
xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Script>
        <![CDATA[
            import air.desktop.URLFilePromise;
           
            import flash.desktop.Clipboard;
            import flash.desktop.ClipboardFormats;
            import flash.desktop.NativeDragManager;

            private var clipboard:Clipboard = new Clipboard();
           
            protected function onDragOut(event:MouseEvent):void
            {
                var items:Array = fileData.selectedItems;
                var promises:Array = new Array();
               
                for each (var item:Object in items)
                {
                    var filePromise:URLFilePromise = new
URLFilePromise();
                    var request:URLRequest = new
URLRequest(item.url);
                   
                    filePromise.request = request;
                    filePromise.relativePath = item.name;
                    promises.push(filePromise);
                }
               
               
clipboard.setData(ClipboardFormats.FILE_PROMISE_LIST_FORMAT,
promises);
                NativeDragManager.doDrag(fileData, clipboard);    
            }
           
            private function
onDragOutComplete(event:NativeDragEvent):void
            {
                trace( "onDragOutComplete" );
            }            

        ]]>
    </fx:Script>

    <fx:Declarations>
        <mx:ArrayCollection id="arrColl">
            <mx:source>
                <fx:Array>
                    <fx:Object name="rhall.jpg"
url="http://a1.twimg.com/profile_images/57117466/robert_m_hall_bio_photo_big_normal.jpg"
/>
                    <fx:Object name="bobjim.jpg"
url="http://a1.twimg.com/profile_images/51723308/ryancampbell3_normal.jpg"/>
                    <fx:Object name="jenschr.jpg"
url="http://a1.twimg.com/profile_images/43222252/jenschr_mugshot3_normal.jpg"/>
                    <fx:Object name="adamflater.jpg"
url="http://a1.twimg.com/profile_images/21503622/Photo_8_normal.jpg"/>
                    <fx:Object name="reboog711.jpg"
url="http://a1.twimg.com/profile_images/16984682/DSCF0044_normal.jpg"/>
                </fx:Array>
            </mx:source>
        </mx:ArrayCollection>
    </fx:Declarations>
    
    <mx:DataGrid id="fileData" dragEnabled="true"
                 dataProvider="{arrColl}"
                 mouseMove="onDragOut(event)"
                 nativeDragComplete="onDragOutComplete(event)">
        <mx:columns>
            <mx:DataGridColumn dataField="name" />
            <mx:DataGridColumn dataField="url"/>
        </mx:columns>
    </mx:DataGrid>
    
</s:WindowedApplication>


To learn more about this feature and more order AdvancED Flex 4


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes