Copy link to clipboard
Copied
I have a flash widget that lets a user select a file and processes its contents in the browser using JavaScript.
The relevant bits of the Flash widget are:
package read {
public class FileReader extends Sprite {
private var file:FileReference = null;
private static var FILE_TYPES:Array = [new FileFilter("Text File", "*.txt;*.text;*.xml;*.rtf;*.csv;*.html;*.css;")];
/**
* Opens the dialog box; connected via Flash Button click
*/
public function beginSelection() {
file = new FileReference();
file.addEventListener(Event.COMPLETE, loadHandler);
file.browse(FILE_TYPES);
}
private function loadHandler(event:Event):void {
var data:ByteArray = this.file.data;
var text:String = data.readUTFBytes(data.bytesAvailable);
file = null;
ExternalInterface.call("fr_onData", text);
}
}
}
This gets compiled into reader.swf, and the HTML is:
<embed width="90px" height="21px" wmode="opaque" scale="noscale" pluginspage="http://www.adobe.com/go/getflashplayer" id="reader" src="/app/jsp/js/at/ext/file/reader/reader.swf?dummy=0.16023823010899152" type="application/x-shockwave-flash">
And the JavaScript is:
window.fr_onData = function (data) {
// TODO: add business logic
};
The problem is when the selected file has escaped quotes.
My file is:
{
"companyId": 32,
"companyName": "ABC \" Construction Ltd."
}
This is valid JSON, but I get the following error in the JavaScript console when I select the file:
SyntaxError: missing ) after argument list
...nyName\": \"ABC \\" Construction Ltd.\"\r\n}\r\n")) ; } catch (e) { "<undefined
Note that the error message has: \"ABC \\\" Construction Ltd.\". \" got converted into \\\" by the Flash object. It's the extra backslash is what messes things up. Changing the file to escape the backslash works -- but this is invalid JSON, so I don't want to / can't do that:
{
"companyId": 32,
"companyName": "ABC \\" Construction Ltd."
}
Suggestions? Why is Flash escaping the backslash?
Have something to add?