2 Replies Latest reply on Oct 13, 2010 11:18 PM by dantheinc

    Writing to a text file using adobe air app in flex 3

    dantheinc

      Hi im trying to get my component (shopping cart), to write to a text file,(shoppingcart.txt), on the desktop.  Not sure exactly what to do, its not passing the information its just writing in null:nan just wanted to pass in name and price.  The following is the code for the main app after this will be the shopping cart component.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="comp.*"
          creationComplete="getData.send();">
         
          <mx:Script>
              <![CDATA[
                 
                  import mx.rpc.events.FaultEvent;
                  import mx.rpc.events.ResultEvent;
                  import mx.events.ItemClickEvent;
                  import mx.events.CollectionEvent;
                  import mx.collections.ArrayCollection;
                  import valueObjects.*;
                 
                  [Bindable]
                  private var blurayArray:ArrayCollection=new ArrayCollection();
                  [Bindable]
                  private var equipArray:ArrayCollection=new ArrayCollection();
                  [Bindable]
                  private var equipmentArray:ArrayCollection=new ArrayCollection();
                 
                 
                  private var total:Number=0;
                 
                 
                  private function resultHandler(evt:ResultEvent):void
                  {
                      blurayArray=evt.result.device.bluray.machine;
                      compBluraydvd.dgrBluraydvd.dataProvider=blurayArray;
                  }
                 
                  private function faultHandler(evt:FaultEvent):void
                  {
                      var faultInfo:String="fault code: "+evt.fault.faultCode+"\n\n";
                      faultInfo+="fault string: "+evt.fault.faultString+"\n\n";
                      mx.controls.Alert.show(faultInfo,"Fault Information");       
                  }
                 
                  private function addToBluDisplay():void
                  {
                      var price:Number=Number(compBluraydvd.dgrBluraydvd.selectedItem.price);
                      var io:String=compBluraydvd.dgrBluraydvd.selectedItem.io;
                      var name:String=compBluraydvd.dgrBluraydvd.selectedItem.name;
                      total=total+price;
                     
                      var theBluRayDVD:BluRayDVD=new BluRayDVD(name,io,price);
                      equipArray.addItem(theBluRayDVD);   
                     
                      compShoppingcart.lstCart.dataProvider=equipArray;   
                      compShoppingcart.lblTotal.text="Total $"+total;
                     
                  }
                  private function addToTvDisplay():void
                  {
                      var price:Number=Number(compTv.dgrTv.selectedItem.Price);
                      var resolution:String=compTv.dgrTv.selectedItem.Resolution;
                      var name:String=compTv.dgrTv.selectedItem.ProductName;
                 
                      total=total+price;
                     
                      var theTv:Tv=new Tv(name,resolution,price);
                      equipArray.addItem(theTv);   
                     
                      compShoppingcart.lstCart.dataProvider=equipArray;   
                      compShoppingcart.lblTotal.text="Total $"+total;
                 
                     
                  }
                  private function saveShoppingCart():void
                  {
                      var price:Number=Number(compShoppingcart.lstCart.selectedItem.Price);
                      var name:String=compShoppingcart.lstCart.selectedItem.ProductName;
                     
                     
                      var theEquipment:Equipment=new Equipment(name,price);
                      equipmentArray.addItem(theEquipment);
                  }
                 
                 
              ]]>
          </mx:Script>
          <mx:HTTPService id="getData" url="xml/bluray.xml" result="resultHandler(event);"
              fault="faultHandler(event);"/>
         
              st

       

          <mx:Style source="css/sim3.css"/>
          <mx:VBox width="949" height="702" backgroundColor="#BCAD84" horizontalAlign="center">

       

              <mx:Label text="DIRT CHEAP ELECTRONICS" width="518" fontSize="28" fontFamily="Georgia" textAlign="center" fontWeight="bold"/>
                  <mx:LinkBar dataProvider="viewstack1" borderStyle="solid" width="836" fontSize="19" fontFamily="Times New Roman" color="#D52113"/>
                      <mx:ViewStack id="viewstack1" resizeToContent="true" height="600" width="830" creationPolicy="all">
                             
                          <ns1:home id="home" label="Home" width="900" height="602"/>
                          <ns1:bluraydvd id="compBluraydvd" label="BluRaydvd" width="820" height="550" backgroundColor="#ECEEB3" addBluDisplay="addToBluDisplay();"/>
                          <ns1:tv id="compTv" label="Tv" width="820" height="550" backgroundColor="#F2F3D6"  addTvDisplay="addToTvDisplay();"/>
                          <ns1:audio id="audio" label="Audio" width="820" height="550" backgroundColor="#EFF2A6"/>
                          <ns1:shoppingcart id="compShoppingcart" label="Shopping Cart" width="820" height="550" backgroundColor="#ECEF99" saveCart="saveShoppingCart();"/>
                             
                             
                      </mx:ViewStack>
                         
         
          </mx:VBox>
         
      </mx:WindowedApplication>

       

      It writes into the shopping cart lstCart just fine, im having trouble exporting the information to a text file.

       

      This is the Shopping Cart Component

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="750" height="612" borderColor="#7FC2F1"
          color="#3C290B" horizontalAlign="center" verticalAlign="top" backgroundColor="#ECEEAC" label="Shopping Cart">
          <mx:Metadata>
              [Event(name="saveCart", type="flash.events.Event")]
          </mx:Metadata>
         
          <mx:Script>
              <![CDATA[
                  import mx.messaging.AbstractConsumer;
                  import mx.collections.ArrayCollection;
                  import mx.events.*;
                  import valueObjects.*;
                 
                  private var file:File;
                  private var path:String="files/shoppingcart.txt";
                  private var stream:FileStream;
                  private var total:Number=0;
                  private var i:Number;
                  [Bindable]
                  private var equipmentArray:ArrayCollection=new ArrayCollection()
                 
                  public function saveShoppingCart():void
                  {
                      var eventObj:Event=new Event("saveCart");
                      dispatchEvent(eventObj);
                  }
                 
                  private function addToFile():void
                  {
                      try
                      {
                          file=File.desktopDirectory.resolvePath(path);
                          //declare a string to add to the file
                          var outputString:String="";
                          //declare variables to get data from equipArray.
                          var  price:Number, name:String;
                         
                         
                          for(i=0;i<equipmentArray.length;i++)
                          {
                              name=equipmentArray[i].name;
                              price=equipmentArray[i].price;
                             
                          }
                         
                         
                         
                         
                         
                          outputString=name+":"+price;
                          outputString=outputString.replace(/\n/g, File.lineEnding);
                          stream=new FileStream();
                          stream.open(file, FileMode.WRITE);
                          stream.writeUTFBytes(outputString);
                          stream.close();
                         
                      }           
                      catch(error:Error)
                      {
                          lblFile.text=error.message;
                      }
                  }
                     //use equipment array to write to the file   
                  /*arrayInfo["io"]=io;
                  arrayInfo["Price"]=price;
                  arrayInfo["ProductName"]=name;
                  arrayInfo["Resolution"]=resolution;
                         
                  equipArray.addItem(arrayInfo);*/
                     
                 
              ]]>
          </mx:Script>
         
              <mx:Label text="SHOPPING CART" textAlign="center" width="259" height="44" fontWeight="bold" fontSize="22" color="#3A404C"/>
              <mx:Panel width="668" height="515" layout="vertical" horizontalAlign="center" verticalAlign="top">
                  <mx:HBox height="384">
                      <mx:List width="583" height="378" id="lstCart"></mx:List>
                     
                     
                  </mx:HBox>
                  <mx:Label text="Label" id="lblFile"/>
                      <mx:Label text="Total $" width="543" height="22" id="lblTotal"/>
                  <mx:HBox>
                     
                     
                      <mx:Button label="Save Cart" id="btnSave" click="addToFile();"/>
                  </mx:HBox>

       

              </mx:Panel>
             
         
         
         
         
         
      </mx:VBox>

       

      If anyone has any suggestions on how to get this to write properly to the text file,  ive tried passing in a new array to read the information from there but im going wrong somewhere.  This is for a project for second year basic web programming, so knowledge is limited.

      would appreciate any help.

       

      Thankyou in advance