使用 URLLoader 时出现意外的 Flash 安全异常 [英] Unexpected Flash Security Exception When Using URLLoader

查看:38
本文介绍了使用 URLLoader 时出现意外的 Flash 安全异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要完成的是使用 URLLoader 类和 URLRequest 将一些二进制数据(特别是表示 PNG 图像的 ByteArray)上传到服务器.

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.

当我将 URLRequest 的 contentType 属性设置为multipart/form-data"而不是默认值时,对 urlLoader.load() 的调用会导致安全例外.

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.

当我将 contentType 属性保留为默认值时,它工作正常,但需要很长时间(与 PNG 文件的长度成比例)将文件上传到服务器.

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?

请注意,我的 SWF 是从开发服务器提供的,而不是本地文件系统(准确地说是 Google App Engine 开发服务器).

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).

代码如下:

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);
});

推荐答案

contentType 可能不是指您发送的数据,而是指您接收的数据.尝试设置 requestHeaders,它应该可以工作:

It could be possible that contentType does not refer to what data you send, but to what data you receive. Try to set the requestHeaders, that should work:

urlRequest.requestHeaders.push(new URLRequestHeader('Content-type', 'multipart/form-data'));

另外,我在我的一个项目中找到了一段代码.该代码运行并使用 POST 将一些二进制 JPEG 数据发送到服务器.我前段时间做了它,我无法解释为什么我这样做,但也许它有帮助.我按原样粘贴它:

Also, I've found a piece of code where in one of my projects. The code works and sends some binary JPEG data to the server, using POST. I dit it some time ago and I can't explain why I did the things this way, but maybe it helps. I'm pasting it as is:

function sendData(submitPath:String, descriere:String):void {
    // building the url request for uploading the jpeg to the server
    var header:URLRequestHeader = new URLRequestHeader('Content-type', 'application/octet-stream');
    var jpgURLRequest:URLRequest = new URLRequest(submitPath+'/id/'+player.id+'/path/'+player.contentPath.replace('/','')+'/width/'+player.videoWidth+'/height/'+player.videoHeight+'/descriere/'+descriere+'/timp/'+time);
    jpgURLRequest.requestHeaders.push(header);
    jpgURLRequest.method = URLRequestMethod.POST;
    jpgURLRequest.data = screenShot;

    // sending the data to the server
    var sender:URLLoader = new URLLoader();
    sender.load(jpgURLRequest);
}

这篇关于使用 URLLoader 时出现意外的 Flash 安全异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆