Skip to main content
markc888
Inspiring
Oct 19, 2011 9:43 AM UTC
Answered

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

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 is closed to new replies. Start a new post to keep the conversation going.
Wespen1Correct answer
Participant
Oct 19, 2011 10:07 AM UTC

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");

            }

}

4 replies

Wespen1Correct answer
Participant
Oct 19, 2011 10:07 AM UTC

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");

            }

}

Translate
Participating Frequently
Oct 19, 2011 4:16 PM UTC

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.

Translate