有没有办法通过Flex的HttpService接收图像文件?

时间:2011-06-30 22:10:58

标签: php flex actionscript-3 httpservice

我编写了这段代码,使用 PHP脚本将数据发送到 Flex

这是Flex代码:

<s:HTTPService id="imageService" url="http://localhost/bookView/amfphp/services/ImageServer/showImage.php" 
                   useProxy="false" 
                   method="POST"
                   result="handleImageResult(event)" 
                   fault="handleFault(event)" 
                   showBusyCursor="true">
        <s:request xmlns="">
            <bdla>"lashf"</bdla>
        </s:request>
    </s:HTTPService>

这是PHP代码:

        public function returnRandomImage(){
            $contents = file_get_contents("images/code_complete2.png");
            header('Content-Type: image/png');

            return $contents;
        }

事情是:我真的对使用PHP发送图像文件感兴趣,因此我可以渲染它并在Flex中使用它。但是,当我.send()这个HttpService时,我得到的只是一个带有此消息的错误事件:(我已经尝试了 header()函数和没有< / strong> it)。

(mx.messaging.messages::AcknowledgeMessage)#0
 body = "PNG"

就是这样。希望有人能提供帮助。 如果没有办法为此目的使用HttpService(即发送图像文件),那么怎么办呢?我在一个应用程序中看过它,所以我很肯定它可以做到。

编辑也添加了PHP代码。

2 个答案:

答案 0 :(得分:2)

您可以使用Base64对图像进行编码,这样您就可以将其发送并以文本形式接收。

http://www.google.es/search?sourceid=chrome&ie=UTF-8&q=as3+base64+encoder

答案 1 :(得分:0)

我将假设您在flexHTTPservice请求时尝试将图像从php发送到flex

你必须确保在php端正确设置标题

// this method should be used if you have the image inside a database
    header('Content-type: image/png');
    echo $image;
//if the image is just an image on the server then you just need to point the HTTPService to the url "http://mydomain.com/testimage.jpg"




如果您要将图像从Flex发送到PHP,那么您必须使用它来获得最佳结果。

[编辑]

// I would use a loader or the Flex Image component to work with images.
var antiCache:Date = new Date( );
_source = val + '&noCache=' +antiCache.getTime();

_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler )
_loader.load(new URLRequest(encodeURI(_source)));
addChild( _loader )