"Escape" externally loaded file string

0

Problem:

I am trying to pass to the database a String containing the bytes of an image that was loaded externally to put into a BLOB field. As always, the bytes in the image have single quotes ' , double quotes " and backslash \ . It turns out that the backslash is a metacharacter and the quotation marks are delimiters of a literal String, and that's where problems arise. See the Byte example:

  

"ÍäWÓužü'vaŽyt¾'m & ²2 \

When loading these bytes into the String, I need them to be "escaped", like this:

  

\ "ÍäWÓužü'vaŽyt¾ \ 'm & ²2 \\

If I try to load them within the ByteArray object of Flash, the bytes are usually recognized, but for communication with the database, they must be in String format.

Below an example in the code:

var loader:Loader = new Loader();
loader.load(new URLRequest("URL_DA_IMAGEM"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {

    var string:String = loader.content.loaderInfo.bytes.toString();
    var query:String = "INSERT INTO tabela (arquivo) VALUES (' "+string+" ')";
    var bytearray:ByteArray = loader.content.loaderInfo.bytes;

    trace(loader.content.loaderInfo.bytes); //"ÍäWÓužü’vaŽyt¾'m&²2\ - VALOR CORRETO, PORÉM NÃO ESCAPADOS
    trace(string); //"ÍäWÓužü’vaŽyt¾'m&²2\ - VALOR CORRETO, PORÉM NÃO ESCAPADOS
    trace(query); /*Erro pois a string com as aspas simples/aspas duplas não está escapada, 
    logo a query dá erro de syntax: INSERT INTO tabela (arquivo) VALUES (' "ÍäWÓužü’vaŽyt¾'m&²2\ ')
    */

}

Attempts:

  • Use String query passing as parameter ByteArray . But I got error, because ByteArray is not String .
  • Method replace with RegExp does not work: string.replace(/(\')/g, "\'"); because Flash automatically removes the backslash, and does not maintain it.
  • Question:

    Does anyone have a solution?

        
    asked by anonymous 29.08.2014 / 21:58

    1 answer

    2

    A byte array is not a string and should not be treated as such.

    They are just bytes, which can easily be turned from a character with accent by mere chance. If you remove the accent, it will change the format of the bytes, and consequently your file.

    I do not know about ActionScript, but from what I saw it should look something like this:

    var query:SqlStatement = new SqlStatement();
    query.text = "INSERT INTO tabela (arquivo) VALUES (:byteArray)";
    
    query.parameters[":byteArray"] = loader.content.loaderInfo.bytes;
    
        
    29.08.2014 / 22:28