1

在将 Sprite 保存到我的服务器时,我似乎遇到了一个有趣的问题。当我在我的 MAC 上运行 swf(远程)时,它工作正常,但是当我从我的 windows 计算机运行完全相同的 URL 时,我得到一个 #2048 SecurityError。

这是我的 AS3 代码:

    public function saveSprite(s:Sprite):void {
        var bmpData:BitmapData = new BitmapData(s.width, s.height, true, 0xFFFFFF);
        bmpData.draw(s);
        var byteArray:ByteArray = PNGEncoder.encode(bmpData);
        var encodedFile:Base64Encoder = new Base64Encoder();
        encodedFile.encodeBytes(byteArray);

        var data:URLVariables = new URLVariables();
        data.fileData = encodedFile;
        data.fileName = "test.png";
        data.location = "temp/";

        var request:URLRequest = new URLRequest(scriptLocation);
        request.method = URLRequestMethod.POST;
        request.data = data;

        var loader:URLLoader = new URLLoader();
        loader.addEventListener(Event.COMPLETE, errorLog);
        loader.addEventListener(Event.OPEN, errorLog);
        loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, errorLog);
        loader.addEventListener(IOErrorEvent.IO_ERROR, errorLog);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorLog);
        loader.addEventListener(ProgressEvent.PROGRESS, errorLog)

        try {
            loader.load(request);
        } catch (e:*) {
            debug.updateLog(e + "\n");
        }
    }

    private function errorLog(e:*):void {
        debug.updateLog(e + "\n");
    }

AS3 与以下一行 PHP 文件连接:

<?php file_put_contents($_POST['location'] . $_POST['fileName'], base64_decode($_POST['fileData']));

当我在我的 Mac 上运行 SWF 时,输出如下: Log Start [Event type="open" bubbles=false cancelable=false eventPhase=2] [ProgressEvent type="progress" bubbles=false cancelable=false eventPhase=2 bytesLoaded =7 bytesTotal=0] [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=200 responseURL=null] [Event type="complete" bubbles=false cancelable=false eventPhase=2]

在 Windows 上,我得到以下输出: Log Start [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=0 responseURL=null] [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase= 2 文本="错误 #2048"]

对此的任何想法将不胜感激。我自己的猜测是具有文件权限的东西,但是什么?

提前致谢!

[编辑] 它变得更有趣。1. 当我将整个项目移动到不同的服务器时,我在 windows 和 mac 上得到 #2048。2. 如果我将文件权限更改为 777,我在 windows 和 mac 上也会得到 #2048。

4

1 回答 1

1

最后!我已经找到了。

将 crossdomain.xml 添加到我的网络服务器的根目录解决了这个问题。对我来说这似乎很奇怪,因为我访问的 php 文件在同一台服务器上,所以这解决了问题。不仅如此,它还在同一个文件夹中!

似乎在 MAC OSX 下,flashplayer 识别出请求在同一个域内,但在 windows 下却不能这样做。这可以解释为什么在 Windows 下安全错误是 trow。这导致我的 swf 在 MAC 下工作,但不能在 Windows 机器上工作。现在,我已将以下 crossdomain.xml 文件添加到服务器的根目录:

<?xml version="1.0" ?>

<cross-domain-policy>
    <site-control permitted-cross-domain-policies="master-only"/>
    <allow-access-from domain="*.mydomain.com" to-ports="*"/>
    <allow-http-request-headers-from domain="*.mydomain.com" headers="*"/>
</cross-domain-policy>

如果您想使用此解决方案,请将“mydomain.com”更改为您要允许的特定域。在此处阅读有关 crossdomain.xml 以及如何使用它的所有内容:http ://learn.adobe.com/wiki/download/attachments/64389123/CrossDomain_PolicyFile_Specification.pdf?version=1

正如我在问题中所述,捕获了以下错误: [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2048"]

请注意,通常错误包含: [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2048: Security Sandbox Violation : http://www.domain1.com/file.swf can not load数据来自http://www.domain2.com/file .*"]

在我的案例中缺少部分错误的事实可能表明请求了同一域中的文件。

所以 crossdomain.xml 成功了!我希望这个答案会对某人有所帮助。

快乐编码!

于 2012-08-14T19:05:26.113 回答