Search code examples
actionscript-3flashair

How to save a file (mp4 or pdf) loaded with StageWebView to iPhone directory?


I'm using StageWebView to load mp4 & pdf files in my iPhone app (Adobe air 3.2 for iOS, in Flash CS5.5)

Below is the code I'm using to load the pdf file (same code to load an mp4 file).

I need to save the file on the iPhone, so the next time the user opens the app, i'll check if the file exists i'll load it using FileStream.open(filePath, FileMode.READ); Is there a way to save a file loaded using the StageWebView? If not, what's the best way to do this.

import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;

var fPath:String = "http://www.bloomerangs.com/Axor/Water-Bath-Design Museum - short version.pdf";

var webView = new StageWebView();
webView.stage = stage;
webView.viewPort = new Rectangle(0, 32, 320, 480);
webView.addEventListener(Event.COMPLETE, onComplete);
webView.loadURL( fPath );

function onComplete(event:Event):void{
    trace("event = ", event);
    trace("event.target = ", event.target);
}

Thanks in advance! :)


Solution

  • Thanks Pavel fljōt.

    I have used the URLLoader for the file download.

    import flash.events.Event;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.filesystem.File;
    
    var urlLoader = new URLLoader();
    urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
    urlLoader.addEventListener(ProgressEvent.PROGRESS, onLoadFileProgress);
    urlLoader.addEventListener(Event.COMPLETE, onLoadFileComplete);
    urlLoader.load(new URLRequest("http://www.yourdomain.com/videos/myVideo.mp4"));
    
    function onLoadFileProgress(e:ProgressEvent):void{
        var loadedPct:uint = Math.round(100 * (e.bytesLoaded / e.bytesTotal));
        trace_txt.text = loadedPct + "% loaded.";
    }
    
    function onLoadFileComplete(e:Event):void{
        trace_txt.text = "File loaded";
        var file:File = File.documentsDirectory.resolvePath("myVideo_copied.mp4")
        var fileStream:FileStream = new FileStream();
        fileStream.openAsync(file, FileMode.WRITE);
        fileStream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, outputProgressHandler);
        fileStream.addEventListener(Event.CLOSE, onVideoSaveClose);
        fileStream.writeBytes(e.target.data);
        fileStream.close();
    }
    function outputProgressHandler(event:OutputProgressEvent):void{
        if (event.bytesPending == 0){
            trace_txt.text = "File is completely written";
        }
    }
    
    var stageWebView = new StageWebView();
    function onVideoSaveClose(event:Event):void {
        trace("closed");
        stageWebView.stage = this.stage;
        stageWebView.viewPort = new Rectangle(0, 0, 1024, 768);
        var file:File = File.documentsDirectory.resolvePath("myVideo_copied.mp4")
        stageWebView.loadURL(file.url);
    }