【发布时间】:2009-08-13 06:28:05
【问题描述】:
我想要完成的是使用 URLLoader 类和 URLRequest 将一些二进制数据,特别是表示 PNG 图像的 ByteArray 上传到服务器。
当我将 URLRequest 的 contentType 属性设置为“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);
});
【问题讨论】:
标签: actionscript-3 file-upload securityexception urlrequest urlloader