AIR:在应用程序文件夹外部加载本地xml文件

时间:2011-04-22 14:42:52

标签: flex security air local-files

我正在构建一个AIR应用程序,该应用程序将用于编辑另一个应用程序的幻灯片(也由我构建,但不在AIR中构建)。幻灯片和其他数据在几个xml文件中定义,这些文件位于另一个应用程序的程序文件夹中。尝试将这些加载到我的AIR应用程序时,我收到以下错误:

fault(mx.messaging.messages::ErrorMessage)#0
  body = ""
  clientId = "DirectHTTPChannel0"
  correlationId = "D67CFDDA-2A2E-9D8B-21DE-7D9FB4B72167"
  destination = ""
  extendedData = (null)
  faultCode = "Server.Error.Request"
  faultDetail = "Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: app:/Users/path/to/folder/languages.xml" errorID=2032]. URL: /Users/path/to/folder/languages.xml"
  faultString = "HTTP request error"
  headers = (Object)#1
    DSStatusCode = 0
  messageId = "F54EACC8-22F3-3718-9106-7D9FB4E0649C"
  rootCause = (flash.events::IOErrorEvent)#2
    bubbles = false
    cancelable = false
    currentTarget = (flash.net::URLLoader)#3
      bytesLoaded = 0
      bytesTotal = 0
      data = ""
      dataFormat = "text"
  errorID = 2032
  eventPhase = 2
  target = (flash.net::URLLoader)#3
  text = "Error #2032: Stream Error. URL:    app:/Users/path/to/folder/languages.xml"
  type = "ioError"
timestamp = 0
timeToLive = 0

这就是我编码的方式:

<?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" creationComplete="windowedapplication1_creationCompleteHandler(event)">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {
            this.languages.addEventListener(FaultEvent.FAULT, this.faultHandler);
            this.languages.addEventListener(ResultEvent.RESULT, resultHandler);

            this.languages.send();
        }

        private function faultHandler(event:FaultEvent):void
        {
            trace("fault" + event.message);
        }

        private function resultHandler(event:ResultEvent):void
        {
            trace("success" + event.result);
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <s:HTTPService id="languages" url="/Users/path/to/folder/languages.xml"/>
</fx:Declarations>
</s:WindowedApplication>

如何从这些文件中读取?该应用程序不需要与本地文件进行任何通信。

感谢J_A_X的回答,我将creationCompleteHandler更改为:

var file:File = new File("/Users/path/to/folder/languages.xml");

var fs:FileStream = new FileStream();
fs.open(file, FileMode.READ);
var text:String = fs.readUTFBytes(file.size);
trace(text);

就像魅力一样。

1 个答案:

答案 0 :(得分:1)

HTTPService用于HTTP通信。获取本地文件不是HTTP。您想查看File类来打开/读取您的xml。