如何在 Flex 中将文件转换为 ByteArray? [英] How can I convert a file to a ByteArray in Flex?

查看:22
本文介绍了如何在 Flex 中将文件转换为 ByteArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将服务器上的文件 (pdf/excel/ppt) 转换为 ByteArray.我想这样做的原因是向用户显示打开/保存的对话框,我必须将 Content-Type 设置为 octet-stream.仅使用 navigateToURL() 就可以正常显示对话,但对于 pdf 而言,它是用户的本地浏览器设置.对于 URLRequest,我必须将数据设置为 ByteArray.我正在尝试使用位于此处的代码:

I was wondering how to convert a file I have on my server (pdf/excel/ppt) to a ByteArray. The reason I want to do this is to display the dialogue open/save as to the user and I must set the Content-Type to octet-stream. The dialogue shows fine with just navigateToURL() but for pdf's it is the user's local browser setting. For a URLRequest I must set the data as a ByteArray. I'm trying to use the code located here:

使用 Flex 进行自定义打印

推荐答案

如果您的目标是 Flash Player 10,这应该可行:

Provided you're targeting Flash Player 10, this should work:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="loadFile()">

    <mx:Script>
        <![CDATA[

            private var loadedFile:ByteArray;

            private function loadFile():void
            {
                var request:URLRequest = new URLRequest("http://test.enunciato.org/sample.pdf");
                var urlLoader:URLLoader = new URLLoader(request);

                urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete);
                urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
                urlLoader.load(request);
            }

            private function onURLLoaderComplete(event:Event):void
            {
                loadedFile = event.target.data;
            }

            private function saveLoadedFile():void
            {
                var file:FileReference = new FileReference();
                file.save(loadedFile, "SampleDoc.PDF"); 
            }

        ]]>
    </mx:Script>

    <mx:Button label="Save Image" horizontalCenter="0" verticalCenter="0" click="saveLoadedFile()" />

</mx:Application>

假设您首先加载文件——在本例中,加载操作发生在 creationComplete 上,并且字节存储在加载文件中——当用户单击保存图像"时,字节应该准备好保存.测试并验证了示例的工作原理.希望能帮到你!

Assuming you load the file first -- in this example, the load operation happens on creationComplete, and the bytes get stored off in loadedFile -- the bytes should be and ready for saving when the user clicks Save Image. Tested and verified the example works. Hope it helps!

这篇关于如何在 Flex 中将文件转换为 ByteArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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