1

I'm getting all data from XML file to Flash. All data in XML file will be displayed in the Flash.

My code in flash:

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("salesAchievementXML.xml"));

function LoadXML(e:Event):void 
{
    xmlData = new XML(e.target.data);
    LoadSales(xmlData);
}

function LoadSales(imageinput:XML):void 
{
    var _salesInfo:XMLList = imageinput.SalesAchievement; 
    var _salesDetails:XMLList = _salesInfo;

    var _salesImage:XMLList  = _salesDetails.fldUserImage;
    ncounter=0;
    for each (var _salesImageElement:XML in _salesImage) 
    {
        _ArrSalesImage[ncounter] = _salesImageElement;
        ncounter++;
    }
}

Sample XML: [ I just screenshot it because the binary is so long]. enter image description here

HEre's the template I'm gonna use: enter image description here

so my problem is: The is a binary type from webservice. Then I'm planning to display the in the box of image. But the problem is, is there a way to display an binary image to flash or it should be converted? If converted, please help me how to convert it..

Thanks!

6
  • Is the image data base64 encoded? Commented Aug 12, 2014 at 2:43
  • @JasonSturges, yes..I use base64 Commented Aug 12, 2014 at 2:50
  • Create image from data-in-uri (base64-encoded PNG) in ActionScript Commented Aug 12, 2014 at 3:59
  • @JasonSturges, thanks for the helping but what is the meaning of this one by.blooddy.crypto? Commented Aug 12, 2014 at 7:09
  • An example cryptography library to decode base64 to a byte array, which you could then display with a loader. Another option would be mx.utils.Base64Encoder. Commented Aug 12, 2014 at 7:14

1 Answer 1

1

To decode the base64 string, you'll need a cryptography library of some kind.

Or, with a small alteration you can use the decoder from Flex.

From the root of your FLA, put this file in a 'mx/utils' folder, as in:

folder-structure

mx.utils.Base64Decoder.as (altered for use with Flash pure ActionScript)

////////////////////////////////////////////////////////////////////////////////
//
//  Licensed to the Apache Software Foundation (ASF) under one or more
//  contributor license agreements.  See the NOTICE file distributed with
//  this work for additional information regarding copyright ownership.
//  The ASF licenses this file to You under the Apache License, Version 2.0
//  (the "License"); you may not use this file except in compliance with
//  the License.  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

package mx.utils
{

import flash.utils.ByteArray;

/**
 * A utility class to decode a Base64 encoded String to a ByteArray.
 *  
 *  @langversion 3.0
 *  @playerversion Flash 9
 *  @playerversion AIR 1.1
 *  @productversion Flex 3
 */
public class Base64Decoder
{
    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Constructor.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function Base64Decoder()
    {
        super();
        data = new ByteArray();
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
     * Decodes a Base64 encoded String and adds the result to an internal
     * buffer. Strings must be in ASCII format. 
     * 
     * <p>Subsequent calls to this method add on to the internal
     * buffer. After all data have been encoded, call <code>toByteArray()</code>
     * to obtain a decoded <code>flash.utils.ByteArray</code>.</p>
     * 
     * @param encoded The Base64 encoded String to decode.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function decode(encoded:String):void
    {
        for (var i:uint = 0; i < encoded.length; ++i)
        {
            var c:Number = encoded.charCodeAt(i);

            if (c == ESCAPE_CHAR_CODE)
                work[count++] = -1;
            else if (inverse[c] != 64)
                work[count++] = inverse[c];
            else
                continue;

            if (count == 4)
            {
                count = 0;
                data.writeByte((work[0] << 2) | ((work[1] & 0xFF) >> 4));
                filled++;

                if (work[2] == -1)
                    break;

                data.writeByte((work[1] << 4) | ((work[2] & 0xFF) >> 2));
                filled++;

                if (work[3] == -1)
                    break;

                data.writeByte((work[2] << 6) | work[3]);
                filled++;
            }
        }
    }

    /**
     * @private
     */
    public function drain():ByteArray
    {
        var result:ByteArray = new ByteArray();

        var oldPosition:uint = data.position;    
        data.position = 0;  // technically, shouldn't need to set this, but carrying over from previous implementation
        result.writeBytes(data, 0, data.length);        
        data.position = oldPosition;
        result.position = 0;

        filled = 0;
        return result;
    }

    /**
     * @private
     */
    public function flush():ByteArray
    {
        if (count > 0)
        {
            throw new Error();
        }
        return drain();
    }

    /**
     * Clears all buffers and resets the decoder to its initial state.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function reset():void
    {
        data = new ByteArray();
        count = 0;
        filled = 0;
    }

    /**
     * Returns the current buffer as a decoded <code>flash.utils.ByteArray</code>.
     * Note that calling this method also clears the buffer and resets the 
     * decoder to its initial state.
     * 
     * @return The decoded <code>flash.utils.ByteArray</code>.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function toByteArray():ByteArray
    {
        var result:ByteArray = flush();
        reset();
        return result;
    }

    //--------------------------------------------------------------------------
    //
    //  Private Variables
    //
    //--------------------------------------------------------------------------

    private var count:int = 0;
    private var data:ByteArray;
    private var filled:int = 0;
    private var work:Array = [0, 0, 0, 0];

    private static const ESCAPE_CHAR_CODE:Number = 61; // The '=' char

    private static const inverse:Array =
    [
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
        64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
        64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
    ];
}

}

Now, you can decode a string and convert it to a byte array:

import flash.display.Loader;
import flash.utils.ByteArray;
import mx.utils.Base64Decoder;

var decoder:Base64Decoder = new Base64Decoder();
decoder.decode("iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAKqUlEQVRYhe2YWYwc13WGv1q7urt6n+nZF5IzJDWkSCoiRY1MiZToBbYUCrJgx7ADIwECI0igLDYCPSuAEyR5SWIndgzHeTJhSIptGJZs05YFm9o4EmXOkCJFzr5vvVVXd1XXngfSgRWK9JBiHgLkAPVyq07d7/73nnvOvfD/9sFMuF3H+3skKZ9JHm1vSx0z6va7a5vGs28sB/6dhAOQb9cxm058ae9w1zOdxYwmSlL00isX8ywbX72TcADSrToc6pLUu/tSX9s92PG0piqy7wd0FDOC6/kHM1Hj+elKUPvN7z8+kto5kJESsxW/fjuA4q06FDLxPxrozP1hFEVXfyAKOI5HPqsXQ8QT9/e8d8zdxcy32nKJv//s/sTt8N36FKcS6p/HNUVuWA5hGFE2AtYqBo7vk23T/85crP4UuATw8eHYtu6O3IfWNqhsrDXvLOAT+3V517bs59WYMry2YT4/caV69o3lgGrT1StzJbSYS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uWAE3bq166vI4aRbSlFQICljyBqigS+QLHWvMUSiabywYHExErb9k88lGFpZLGTyYHueLto+lWkFUVgJRoIvgB1ZJFreFhCD4NP7wugrcE+MWjhb69946OnH13BjVwyHel6RsY5EzV8srdH1GSmsTDjyxiz8yxoy+FFpMorEQ8ezrBy+LH8EIBz2nhlC+ihTXsgYdpqgpWrUmrbjNXcwgyghGEXLllwKdG9YHBod2viAK9hb6+yaX5mRnRjR0/OHpYPjv5n8pYwyKT7YEwoFiIszJXoewEpDWRvriDuV4hcAwU8zJ5e/qK7zuzob36yFRml/KyW6Y/FeKkBL9ihX+dUIXGLQE+NaoLqUzuH/q3D/euLMzOXpo4+yCwUezses5znScPDHXw1rkpqsk0ZdPHWjJYnCxRtXy0uEw2o5Hd/BGRU19rtuyfXzGCM14QOZ2pxYv51uros2WpLyH5AYLwL6mY8NXvj9vvy3EzBfu7ewce1hJJjGr5G8D6V15v8EzH6nzPwBCKItWSjXca5XJP76uTIY8VVCpNn5YX0vA8ujMylrHx9amK/yugCYwDC6tm0Fo1g1/329rVJoevzt/4WvFm5ZaSzuWVMAho2ZZyTdV8vq3jU3WjQq3pTpZr1ZOp2uve98cjHElAick0nIBQELC8gIYXVYAq8AJwAagDLmBde8LLpZvfed4wkxzuUxu5QvtDnuvs2LnnwMMjBw79Xs/A9mcEQWwrlUrlb7749j8v14MZLTSXNH9z++SGrx87kCOeiHHPcIZfTlWab6+6pxw/+u41yNuyGwKOLbnBsFY9HdPivVPvnk8360bcbjaMRr128vSvLn/xjTnrFHDRdKMLtutMRq4lbBhWerBNNc/N18xXFqyJBSP4xzBi4XbhYAvl1lOj+nVtX3n9+oArJkW252WG8zIVO2SuFvDOxm+/Ofg/b/8FrV74eebdEP8AAAAASUVORK5CYII=");

var bytes:ByteArray = decoder.toByteArray();
var loader:Loader = new Loader();
loader.loadBytes(bytes);

addChild(loader);

The above code produces:

screen-capture

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.