views:

283

answers:

1

Hello,

I'm not familiar with Adobe Air and I am uploading a file to a server. For simple testing purposes I have hard coded an upload URL (Specifically a url to a upload directory on the server to which the amf channels point) into the code. Is there a way in adobe air to get this server url at run time?

Or does the question not make any sense because there is a better way of doing it?

A: 

It sounds to me as though you want the upload URL to be dynamic. In this case you could use many methods of retrieving such data at run-time. I would use XML if I was you and I will illustrate how to do so in the following.

//THE XML - Place in same folder as the FLA and name setup.xml
<?XML version="1.0" encoding="utf-8"?>
<xml>
    <upload url="path/to/file" />
</xml>

Now that is a very simple XML file, it could just as easily include information for background music, CSS formatted text wrapped in CDATA, or any other type of data that you would like to hold and retrieve.

//The ActionScript 3.0

package
{
    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;

    public class TestDocClass extends Sprite
    {
        private var _xml:XML;
        private var _xmlLoader:URLLoader;
        private var _xmlRequest:URLRequest;

        public function TestDocClass():void
        {
            _xmlLoader = new URLLoader();
            _xmlRequest = new URLRequest('setup.xml');
            _xmlLoader.addEventListener(Event.COMPLETE, onXMLComplete, false, 0, true);
            _xmlLoader.load(_xmlRequest);
        }
        private function onXMLComplete(e:Event):void
        {
             _xmlLoader.removeEventListener(Event.COMPLETE, onXMLComplete, false);

            //Create the XML object and insert the data.
            _xml = new XML(e.target.data);

            //Now to access the upload line we use its Namespace of "upload"
            //The @ symbol specifies we are retrieving an attribute.

            trace(_xml.upload.@url);
        }
    }
}

So anywhere you need that upload destination you merely call _xml.upload.@url or you can set a variable to that value when the data arrives (var uploadURL:String = _xml.upload.@url).

Check out the best tutorials which are by Lee Brimelow at http://www.gotoandlearn.com and his blog at http://theflashblog.com

My works are at http://www.pixeldev.net

Brian Hodge

related questions