如何将文件转换为Flex中的ByteArray? [英] How can I convert a file to a ByteArray in Flex?
问题描述
我想知道如何将我的服务器上的文件(pdf / excel / ppt)转换为ByteArray。我想这样做的原因是显示对话框打开/保存为用户,我必须设置内容类型为八位字节流。对话只显示navigateToURL(),但对于pdf,这是用户的本地浏览器设置。对于URLRequest,我必须将数据设置为ByteArray。我试图使用位于这里的代码:
< / p>如果你的目标是Flash Player 10, ?xml version =1.0encoding =utf-8?>
< 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函数onURLLoaderComplete(event:Event):void
{
loadedFile = event.target.data;
$ b $ private function saveLoadedFile():void
{
var file:FileReference = new FileReference();
file.save(loadedFile,SampleDoc.PDF);
}
]]>
< / mx:Script>
< / mx:应用程序>假设你先加载文件 - 在这个例子中,加载操作发生在creationComplete上,并且字节被保存在loadedFile中 - 当用户点击保存图像时,字节应该准备好保存。经过测试和验证的例子工程。希望它有帮助!
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:
解决方案 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>
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屋!