9 Replies Latest reply on Jan 24, 2012 7:05 AM by NaBUru83

    How to upload/save an XML file locally

    NaBUru83

      Hello, I'm developing an amateur videogame on Flex 4, which will be downloadable and run in the local filesystem (like "C:/Games/myGame/myGame.html"). I'm trying to save and load the player records in an XML file, also locally (like "C:/Games/myGame/records.xml).

       

      So far, I've been abled to load the file like this:

       

      var loader : URLLoader = new URLLoader();

      var request : URLRequest = new URLRequest ("records.xml");

      var recordsXml : XML;

      try {

      _ loader.load (request);

      _ recordsXml = new XML (loader.data);

       

      }

      catch (...

       

      After that, I can then extract the data perfectly from recordsXml. However, I can't figure out what code to use to save the modified data. I've tried this:

       

      var request : URLRequest = new URLRequest ("records.xml");

      request.data = recordsXml.toXMLString();

      request.contentType = "text/xml";

      request.method = URLRequestMethod.POST;

      loader.dataFormat = URLLoaderDataFormat.TEXT;

      try {

      _ loader.load (request);

      }

      catch (...

       

      The code runs without errors, but the file doesn't get updated. I've tried saving the data on a second, empy file records2.xml, but it doesn't work either.

       

      What should I do? Thanks!

        • 1. Re: How to upload/save an XML file locally
          Claudiu Ursica Level 4

          I am not sure I follow, what do you mean by donwloadable?  ant the html? in the path, is this web or air app.?

          If it is air you may want to look at the FileStream api. Web FileReference .

           

          C

          1 person found this helpful
          • 2. Re: How to upload/save an XML file locally
            NaBUru83 Level 1

            Hi, Claudiu! The game is a Flex web project, it works in many computers and browsers.

             

            The player will download the self-extractable .exe from my website, extract the files to a folder like "C:/Games/myGame/", and then open the Flex web wrapper "C:/Games/myGame/myGame.html" with any browser. Once the files are extracted, there's no need to be online, all the requiered files will be in "C:/Games/myGame/".

             

            I can open a local file "C:/Games/myGame/records.xml" and load its data. What I'm struggling to do is to save the modified data in the same file.

            • 3. Re: How to upload/save an XML file locally
              Claudiu Ursica Level 4

              I don't think you have access to the disk from within the web wrapper. You can only flush data to a so called "flush cookie" using a SharedObject. If you only want to keep limited information this may be a good choice, though if the user cleans the cookie the info is gone with it. FileReference.save() requires user to save by using the system dialog. You may consider to pack it as air app for the desktop version as an alternative. 

               

              C

              1 person found this helpful
              • 4. Re: How to upload/save an XML file locally
                NaBUru83 Level 1

                Ouch! I converted the project into AIR, but that brings a lot of changes. For example, it requires to install the game. I don't want my games to require installation, I want them to be "extract and play" like my previous ones. And the installation process shows a nasty red warning that says "it may put your system in danger". That's not nice!

                 

                If I can open a local file from the Flex web game, then there must be some way to save the file.

                • 5. Re: How to upload/save an XML file locally
                  DonMitchinson Level 2

                  If you're going to write to a file it needs to be stored in the applicationStorage folder.

                  You have full write access to that folder

                   

                  You can distribute the opening  xml file with your app - it gets placed in your application folder.

                  On first run, copy it to the applicationStorage folder if it doesn't exist.

                  Any changes made by the user can then be saved to the file in the applicationStorage folder.

                  • 6. Re: How to upload/save an XML file locally
                    NaBUru83 Level 1

                    Don, I've found this document that mentions applicationStorage. As I said, I want the project to be web. But that document doesn't mention "web" and pasting "File.applicationStorageDirectory" in my code doesn't reconize File at all. So I guess Flex web projects don't have any of that.

                    • 7. Re: How to upload/save an XML file locally
                      DonMitchinson Level 2

                      It looked to me like your were already looking at the AIR conversion possibility.

                      If you're opening in the file on the user's PC it's a desktop app - so you need to build it as an AIR project.

                       

                      Just because you're opening HTMl/web pages inside the app, it doesn't make it a web application.

                      If you need to store and manipulate local files it'll have to be an AIR application

                       

                      Hope that helps

                      1 person found this helpful
                      • 8. Re: How to upload/save an XML file locally
                        iBr3nt Level 3

                        For web apps, you can use the FileReference class. This allows you to

                        open a dialog to the user to load an external file, or save it to their

                        computer. Here's more details:

                         

                        http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7cf8.html

                         

                        Please note that this only works if the user triggers the action and

                        goes through the file dialog. You can't save or load files without their

                        knowledge from a web app. An AIR desktop app is different, and can

                        load/save files in the background.

                         

                        iBrent

                        • 9. Re: How to upload/save an XML file locally
                          NaBUru83 Level 1

                          Wow, a dialog box! Were I a professional game interface designer, I would get hung in the main square in front of a massive audience for using it. Thank god I'm a game programmer!

                           

                          I've tried FileReference with simple XML files and it worked fine. Thanks to everyone!

                           

                          Just two quick final questions:

                           

                          o- How can I tell the save dialog box (FileReference.save()) to use only a certain file extension? (I did that already with the loading.)

                          o- How can I make the dialog boxes open the game folder (like "C:/Games/myGame/"), rather than the system's default folder?