Skip to main content
markc888
Inspiring
October 19, 2011
Answered

Download pdf or jpg file and save it to the device for offline viewing

  • October 19, 2011
  • 1 reply
  • 3317 views

Hi,

Publishing an AIR for iOS app via Flash CS5.5, would anyone know if is it possible to download pdf or jpg file and save it to the device for offline viewing?

If so, could you give me some pointers as to the classes involved?

Thanks,

Mark

This topic has been closed for replies.
Correct answer Wespen1

Save loader content to byte array then send that byte array to FileStream

Something like:

mcLoader = new URLLoader();

mcLoader.dataFormat = URLLoaderDataFormat.BINARY;

var req:URLRequest = new URLRequest (file);        

mcLoader.addEventListener(Event.COMPLETE, contentLoaded);

mcLoader.load (req);

private function contentLoaded(e:Event):void {

var dataContent:ByteArray = new ByteArray();

dataContent = e.target.data;

var fileName:String = "mylocal.pdf"

var f:File = File.applicationStorageDirectory.resolvePath (fileName);

           //save file to local space

            if (f.exists == false) {

                trace ("file doesnt exist. Saving file");

                var fs:FileStream = new FileStream();

                fs.open(f, FileMode.WRITE)

                fs.writeBytes(dataContent);

                fs.close();

                trace ("saved");

            }

}

1 reply

Wespen1Correct answer
Participant
October 19, 2011

Save loader content to byte array then send that byte array to FileStream

Something like:

mcLoader = new URLLoader();

mcLoader.dataFormat = URLLoaderDataFormat.BINARY;

var req:URLRequest = new URLRequest (file);        

mcLoader.addEventListener(Event.COMPLETE, contentLoaded);

mcLoader.load (req);

private function contentLoaded(e:Event):void {

var dataContent:ByteArray = new ByteArray();

dataContent = e.target.data;

var fileName:String = "mylocal.pdf"

var f:File = File.applicationStorageDirectory.resolvePath (fileName);

           //save file to local space

            if (f.exists == false) {

                trace ("file doesnt exist. Saving file");

                var fs:FileStream = new FileStream();

                fs.open(f, FileMode.WRITE)

                fs.writeBytes(dataContent);

                fs.close();

                trace ("saved");

            }

}

Participating Frequently
October 19, 2011

The FileReference class has functions for downloading files, too. These require user interaction to choose the save location, but that could be what you want anyway.