我想要完成的是使用 URLLoader 类和 URLRequest 将一些二进制数据,特别是表示 PNG 图像的 ByteArray 上传到服务器。
当我将contentType
URLRequest 的属性设置为“multipart/form-data”而不是默认值时,调用会urlLoader.load()
导致安全异常。
当我将该contentType
属性保留为默认值时,它可以正常工作,但需要很长时间(与 PNG 文件的长度成正比)才能将文件上传到服务器。
所以,我的问题是为什么我会得到这个安全异常?我该如何避免呢?
请注意,我的 SWF 是从开发服务器而不是本地文件系统(准确地说是 Google App Engine 开发服务器)提供的。
这是代码:
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);
});