Not yet rated
Tags:



Problem

Using browse for a supported file in AIR 2

Solution

An AIR application that you can browse for a supported file, and watch how the registered application opens the file.

Detailed explanation

The File API exposes a new method called openWithDefaultApplication() in AIR 2 to allow opening a file with the default OS registered program.  When you use this method the file will open using the default application that is registered with the operating system. If the file is executable, then that executable program is launched.
Due to security concerns, AIR prevents you from using the File.openWithDefaultApplication() method to open certain files (see table below) and will throw an exception.


On Windows, AIR prevents you from opening files that have certain file types . On Mac OS, AIR prevents you from opening files that will launch in specific applications. On Linux, AIR prevents you from opening a file that has the executable bit set, and you cannot open a file that will launch in certain applications. Take a look at a sample table that shows examples of file types that are blocked:

 

Type             Windows Application             Mac OS Application             Linux Application

Executable File             exe             executable bit, .app extension             /lib/ld.so

UNIX shell script             sh             Terminal              /bin/bash

UNIX shell script             sh             Terminal              /bin/bash

UNIX ksh shell script             ksh             Terminal              /bin/ksh

The application below allows you to browse for a file in your local system and then open the file with the default application.  Notice the try..catch code, which is needed in cases where a failure occurs due to a file that cannot be opened since there is no default for file type or security reasons.
Note that you cannot communicate with launched process after it's open using the openWithDefaultApplication method.

Create a new AIR application name it OpenFileDefaultApp. See code below:

<?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 flash.events.Event;
            import flash.events.MouseEvent;
            import mx.events.FlexEvent;
           
            private var file:File;
           
            private function
buttonClickHandler(event:MouseEvent):void
            {
                file = new File();
                file.addEventListener(Event.SELECT, onFileSelect);
                file.browseForOpen("Browse for a file:");
            }
           
            private function onFileSelect(event:Event):void
            {
                richText.text =
File(event.currentTarget).nativePath;
                file.load();
               
                try {
                    file.openWithDefaultApplication();
                }
                catch(error:Error) {
                    trace("The file you selected is prohibited and
cannot be opened");
                }
            }

        ]]>
    </fx:Script>
   
    <s:Button id="button"
               label="Click to browse"
               click="buttonClickHandler(event);"
               width="134" />
   
    <s:RichText id="richText" x="0" y="29"/>
   
   
</s:WindowedApplication>



AIR 2.0 also adds a property called downloaded. Setting that property will instruct the OS that the file was downloaded and may result in user's notification first time the file opens up such in Mac OS X or Windows XP SP2 and later. You can set it as follow:

file.downloaded = true;
 

Additionally, you can use file.download( request );  method to download the file and at that case it will be marked as downloaded automatically.

 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