いてづきブログ

情シスでやってみたことの備忘録

ローカルのファイルをデフォルトアプリで開く

<?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[
protected function openWithDefaultApplication(event:MouseEvent):void
{
var f:File = new File();

f.addEventListener(Event.SELECT,function(event:Event):void{
(event.target as File).openWithDefaultApplication();
});

f.browseForOpen("ファイルを選択。");
}
]]>

</fx:Script>

<s:Button label="ファイル選択" click="openWithDefaultApplication(event)"/>

</s:WindowedApplication>

ファイル選択のダイアログは、FileインスタンスのbrowseForOpenで開く。
Fileにselectイベントやcancelイベントのリスナーを登録しておく。

browseForOpenの説明にあるように、event.targetが選択されたfileオブジェクトを指しているので、File型として扱って、openWithDefaultApplicationで開く。
exe(winの場合)なら、アプリがそのまま開く。