flex 加载txt文件 URLLoader

点击按钮,执行btn_downloadModel_clickHandler: 可以获取文本文件的内容

 

protected function btn_downloadModel_clickHandler(event:MouseEvent):void{
       /*@get data from server*/   
    
     var src:String="http://localhost:8080/blazeds/templateFile/ImportVipUser.txt";
     var header1:URLRequestHeader = new URLRequestHeader("Content-Type" ,"application/vnd.ms-excel;charset=utf-8");  
     //var header2:URLRequestHeader = new URLRequestHeader("Accept" ,"application/json");  
     var request:URLRequest = new URLRequest(src);  
     request.method = URLRequestMethod.GET;  
     //request.requestHeaders = [header1 ,header2];  
     request.requestHeaders=[header1];
     
     var loader:URLLoader = new URLLoader();   
     loader.dataFormat = URLLoaderDataFormat.TEXT;   //文本文件,中文会乱码
   //  loader.dataFormat=URLLoaderDataFormat.BINARY;
     loader.addEventListener(Event.COMPLETE,  
      /*function():void{  
       Alert.show('Complete');
       Alert.show(loader.data);
      }*/
      callBack
     );  
     loader.load(request);   
     
   }
   private function callBack(evt:Event):void{
    //Alert.show(evt.currentTarget.data);
    var bytes:ByteArray = ByteArray(evt.currentTarget.data);
    //var xmlStr:String = bytes.readMultiByte(bytes.length,"utf-8");  中文还是乱码
    var xmlStr:String = bytes.readMultiByte(bytes.length,"gbk");  不会乱码
    Alert.show(xmlStr);
    trace(xmlStr);
    var xml:XML = new XML(xmlStr);
    trace(xml.toString());

   }

你可能感兴趣的:(json,xml,Excel,Flex)