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:
query
passing as parameter ByteArray
. But I got error, because ByteArray is not String
. 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?