Flex air 调用本地文件,比如Excel,Word,PDF,Notepad,html 文件
发布时间:2020-12-15 04:50:35 所属栏目:百科 来源:网络整理
导读:?????? 在做本地演示小项目的过程中,需要Flex,AIR操作本地文件,比如Excel,Word,PDF,Notepad 等文件,? 在AIR 2.0以下,主要是使用NativeProcess这个类来实现的,而且还要做相关的配置工作。注意,我这里的工作环境是flex 4.5 .具体实现如下所示: ????? 1.
?????? 在做本地演示小项目的过程中,需要Flex,AIR操作本地文件,比如Excel,Word,PDF,Notepad 等文件,? 在AIR 2.0以下,主要是使用NativeProcess这个类来实现的,而且还要做相关的配置工作。注意,我这里的工作环境是flex 4.5 .具体实现如下所示: ????? 1.这里以简单的操作Notepad为例,如下: package com.floor.screen.redevelop.app { import flash.desktop.NativeApplication; import flash.desktop.NativeProcess; import flash.desktop.NativeProcessStartupInfo; import flash.display.MovieClip; import flash.events.IOErrorEvent; import flash.events.MouseEvent; import flash.filesystem.File; import flash.system.fscommand; public class CallEXE extends MovieClip { private var file:File=new File(); private var nativeProcessStartupInfo:NativeProcessStartupInfo; public function CallEXE():void{ //使用静态属性 NativeApplication.nativeApplication 获取应用程序的 NativeApplication 实例 //指定在关闭所有窗口后是否应自动终止应用程序。 /*当 autoExit 为 true(默认值)时,如果关闭了所有窗口,则应用程序将终止。调度 exiting 和 exit 事件。如果 autoExit 为 false,则必须调用 NativeApplication.nativeApplication.exit() 才能终止应用程序。*/ NativeApplication.nativeApplication.autoExit=true; //调用的文件名称 file=file.resolvePath("C:/windows/notepad.exe"); // C:/windows/notepad.exe //file=file.resolvePath("C:/Users/LONMID/Desktop/flex.bat"); //assertThat.doc //C:/Program Files/UltraEdit/Uedit32.exe trace("file :",file.nativePath); nativeProcessStartupInfo = new NativeProcessStartupInfo(); nativeProcessStartupInfo.executable = file; //fscommand("exec","D:M1clockv1.bat"); } public function runTest():void { var process:NativeProcess = new NativeProcess(); process.start(nativeProcessStartupInfo); } } } 2.操作WORD,EXCEL,PPT文件,如下所示: package com.floor.screen.redevelop.app { import flash.display.Sprite; import flash.desktop.NativeProcess; import flash.desktop.NativeProcessStartupInfo; import flash.events.Event; import flash.events.ProgressEvent; import flash.events.IOErrorEvent; import flash.events.NativeProcessExitEvent; import flash.filesystem.File; public class NativeProcessDemo extends Sprite { public var process:NativeProcess; public function NativeProcessDemo() { if(NativeProcess.isSupported) { setupAndLaunch(); } else { trace("NativeProcess not supported."); } } public function setupAndLaunch():void { var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); var file:File = File.applicationDirectory.resolvePath("C:/Program Files/Microsoft Office/Office12/POWERPNT.EXE"); //C:/windows/notepad.exe //C:/Program Files/UltraEdit/Uedit32.exe //C:Program FilesMicrosoft OfficeOffice12 //WINWORD.EXE nativeProcessStartupInfo.executable = file; var processArgs:Vector.<String> = new Vector.<String>(); processArgs[0] = "C:/Users/LONMID/Desktop/Java编码规范1.ppt" //"C:/Users/LONMID/Desktop/lamp.doc"; //C:UsersLONMIDDesktopinfo.txt //C:UsersLONMIDDesktop //D:/new/8FloorScreenRedevelop/src/_readme.txt // nativeProcessStartupInfo.arguments = processArgs; process = new NativeProcess(); process.start(nativeProcessStartupInfo); process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA,onOutputData); process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA,onErrorData); process.addEventListener(NativeProcessExitEvent.EXIT,onExit); process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR,onIOError); process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR,onIOError); process.closeInput(); } public function onOutputData(event:ProgressEvent):void { trace("Got: ",process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable)); } public function onErrorData(event:ProgressEvent):void { trace("ERROR -",process.standardError.readUTFBytes(process.standardError.bytesAvailable)); } public function onExit(event:NativeProcessExitEvent):void { if(event.exitCode.toString()=="0") { // GameModel.getInstance().state=false; } else if(event.exitCode.toString()=="1") { // GameModel.getInstance().state=true; } this.dispatchEvent(new Event("refresh")); } public function onIOError(event:IOErrorEvent):void { trace(event.toString()); } } } 以下两种方式都需要在相应的配置文件中添加: <supportedProfiles>extendedDesktop</supportedProfiles> 最后打包的时候还要 Signed native installer,而不是Signed AIR package.? 3.操作为PDF文件,如下所示: <?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" initialize="initApp();" showStatusBar="false" width="1558" height="740" > <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <![CDATA[ import flash.html.HTMLPDFCapability; //use HTMLPDFCapability import flash.html.HTMLLoader; //use HTMLLoader import mx.controls.Alert; //use Alert //initialize private function initApp():void { //置顶和拖动效果 // this.stage.nativeWindow.startMove(); //this.stage.nativeWindow.alwaysInFront=true; //check Adobe Reader 8.1 or above capability if(HTMLLoader.pdfCapability==HTMLPDFCapability.STATUS_OK) { var request:URLRequest = new URLRequest("C:/Users/LONMID/Desktop/object-oriented Analysis & Design.pdf");//URLRequest example var pdf:HTMLLoader = new HTMLLoader(); //HTML Control pdf.height = 800; //set pdf height pdf.width = 1280; //set pdf width pdf.load(request); //load pdf container.addChild(pdf); //put pdf in HTML container } else Alert.show("pdf cant display,not Adobe Reader 8.1 and above version"); } private function htmlLoaded(event:Event):void { } ]]> </fx:Script> <mx:HTML id="container" width="1278" height="718"/> </s:WindowedApplication> 4.html文件呈现 <?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" width="1280" height="800" > <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <![CDATA[ import mx.controls.HTML; private function onComplete(evt:Event):void { var document:Object = HTML(evt.currentTarget).htmlLoader.window.document; var anchors:Object = document.getElementsByTagName("a"); if(anchors != null) { // 转载请注明出 处:http://hi.baidu.com/taotao5453 for(var i:Number=0; i < anchors.length; i++) { anchors[i].onmouseup = function():void { var request:URLRequest = new URLRequest(arguments[0].srcElement); navigateToURL(request,"_blank"); } } } } ]]> </fx:Script> <mx:Panel width="95%" height="98%" layout="absolute" horizontalCenter="0"> <mx:HTML id="content" width="100%" location="http://www.baidu.com" height="90%" complete="onComplete(event)" top="70" horizontalCenter="0"/> </mx:Panel> </s:WindowedApplication>结论: 笔者在实现了Flex,AIR操作本地文件的方法的总结,需要对一些需要本地化操作有所帮助。助你好运!!! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |