Flex Mobile using StageWebView to show docs (ppt, excel, word, pdf)

If you want to show documents (ppt, xls, rtf, doc, docx, pdf, etc) in your flex mobile application here is some code that should help. Note, this was tested on iOS. Your mileage may vary on andriod, but I assume it will work just fine. This only works because webkit has viewers for these file formats. No editing, just viewing.

First - How to download a file from a remote server and store it in the application storage directory. You could also bundle these files with your app, but chances are you want to pull in from a remote source and save the files locally for viewing.

    var file:String = “test.xlsx”;  
button.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE,
function(e:Event):void {
var air:File = File.applicationStorageDirectory.resolvePath(file);
var fs:FileStream = new FileStream();
fs.open(air, FileMode.WRITE);
fs.writeBytes(loader.data);
fs.close();
});
loader.load(new URLRequest(“http://wookets.com/"+file));
});


Next show the file - This uses webkit behind the scenes to display the file. So the finished pinching / zooming will replicate safari.

 button.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {  
// show a stage web view
var yOffset:Number = 40;
var stageWebView:StageWebView = new StageWebView;
stageWebView.stage = stage;
stageWebView.viewPort = new Rectangle(0, yOffset, stage.width, stage.fullScreenHeight - yOffset);
var pdf:File = File.applicationStorageDirectory.resolvePath(file);
stageWebView.loadURL(pdf.nativePath);
});


Of note:

There is a method on the File class called openWithDefaultApplication(). It does NOT work on mobile devices. The method will not do anything, just silently fail. Go to ideas.adobe.com and vote this up, because this feature is critical for allowing other apps to be able to open and handle documents.

It’s not a lot of code, but it took me quite too long to figure all of this out. So, hopefully, this helps you.

Btw, if you want a download of some source code, I can zip it up and provide that. Sorry, this one isn’t as demoable as the others.

Published by using 305 words.