tags:

views:

2233

answers:

4

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:

Custom printing with Flex

A: 

I think you can do something like this to fetch the byte array:

private var fileBytes:ByteArray = null;

private static function loadByteArray(url:String):void {
      var loader:Loader = new Loader();

      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, 
      function(e:Event):void {
        fileBytes = e.currentTarget.bytes;
      });
      loader.load(new URLRequest(encodeURI(url)));
}

// static initializer
{ loadByteArray("my url here"); }

Edit: I didn't test the above function. If you want to load bytes directly, I'd suggest using Christian Nunciato's answer, since his is better and actually tested.

However, it sounds more like you want to retrieve a file directly from your server as opposed to doing a three way trip (file comes from the server, you send the bytes back to the server, and then finally they come back to the client). In that case, you may try setting the content type and the content disposition to attachment on the server (e.g. for pdf, this would mean setting content type to "application/pdf" and adding "Content-Disposition: attachment; filename=myFileName.pdf" to your response headers).

mweiss
+2  A: 

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!

Christian Nunciato
A: 

Hi Christian Nunciato

i have read your post, here you just call the fileref.save() method, but there is such method in FileReference, i want same function in my project, can u please guide me,

thanks syeddiwan

syeddiwan
A: 

hi guys, sorry guys currently i am using sdk 3.0 , save method is only available at sdk 3.2, now its working,

syeddiwan