What I am trying to accomplish is to upload some binary data, specifically a ByteArray representing a PNG image, to a server using the URLLoader class in conjunction with URLRequest.
When I set the contentType
property of the URLRequest to 'multipart/form-data' instead of the default, the call to urlLoader.load()
results in a security exception.
When I leave the contentType
property as the default, it works fine, but takes a long time (proportional to the length of the PNG file) to upload the file to the server.
So, my question is WHY am I getting this security exception? And how can I avoid it?
Note that my SWF is being served up from a development server, not the local filesystem (the Google App Engine development server to be precise).
Here is the code:
var pngFile:ByteArray = PNGEncoder.encode(bitmapData);
var urlRequest:URLRequest = new URLRequest('/API/uploadImage');
// With this line of code, the call to urlLoader.load() throws the following security exception:
// 'SecurityError: Error #2176: Certain actions, such as those that display a pop-up window, may only be invoked upon user interaction, for example by a mouse click or button press.'
urlRequest.contentType = 'multipart/form-data';
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = pngFile;
urlRequest.requestHeaders.push(new URLRequestHeader('Cache-Control', 'no-cache'));
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, onUploadComplete);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onUploadError);
NextFrame.addCallback(function () {
urlLoader.load(urlRequest);
});