FLEX / AS3如何打开docx / odt文件

时间:2016-05-11 05:41:44

标签: actionscript-3 flex

我有创建docx或odt文件的功能。创建完成后,我需要在microsoft / libre office中自动打开此文件。如何在flex / as3中编码?

        protected function create003(docType:String, patientID:String):void
        {
            create003Result.token = nhealthReports.create003(docType, patientID);               
        }

         protected function  getFormModuleDataResult_resultHandler(event:ResultEvent):void
        {
            var pathToFile:String;
            pathToFile=create003Result.lastResult; // this is path to created file
          // here i need some code from you
         }



         <nhealthreports:NhealthReports id="nhealthReports"
                                   showBusyCursor="true"/>     
        <s:CallResponder id="create003Result" result="getFormModuleDataResult_resultHandler(event)"/>

1 个答案:

答案 0 :(得分:2)

因此,您需要先将文件下载到用户的计算机,然后再将其打开。这样的事情应该这样做(从我的项目中复制粘贴的东西,所以你可能需要稍微调整一下)。

此外,您的服务器可能需要一个跨域文件,以便您的应用可以从中加载文件。

    private function getFormModuleDataResult_resultHandler(event:ResultEvent):void
    {
        // load file
        var loader:URLLoader = new URLLoader();
        loader.addEventListener(Event.COMPLETE, onLoadingComplete);
        loader.dataFormat = URLLoaderDataFormat.BINARY;
        loader.load(new URLRequest(pathToFile));
    }

    private function onLoadingComplete(event:Event):void
    {
        // get the data as bytearray
        var data:ByteArray = event.target.data;

        // you will probably need to figure this out from your server path or define your own here
        var fileName:String = "MyFilename.doc";

        // create a file under the application storage directory (C:\Users\YOURUSERHERE\AppData\Roaming\RateBook\Local Store)
        // you can store the file anywhere but it is recommended to do it here 
        // as users with restricted access on their machines (non-admin users) might have trouble saving the files elsewhere
        var file:File = File.applicationStorageDirectory.resolvePath(fileName);

        //create a file stream to be able to write the content of the file    
        var fileStream:FileStream = new FileStream();
        //open the file stream and set for Write
        fileStream.open(file, FileMode.WRITE);
        //writes the bytes
        fileStream.writeBytes(data, 0, data.length);
        //close the stream
        fileStream.close();

        // by now the file should be saved to disk, let's open it
        // Naturally this assumes that the user have the file extension (like .doc) associated with the correct program (MS Word)
        file.openWithDefaultApplication();
    }