Eek, I made a rather stupid assumption with AIR, and it seems I may be wrong.
I thought that AIR was able to download files (in my case mp3 files) from a http server and store them in the application storage directory without any user interaction. Essentially it’s a flash program that automatically retrieves podcast files from an RSS Podcast feed for visitors of a gallery to peruse. The machine won’t be online all the time, so offline local backup of the files is necessary. And a dialogue box popping up for vsitors to mess around with must be avoided.
Is this possible or have I just signed my own death warrant? Please don’t make me go back to Zinc!!!
It should be possible to save stuff to application storage, but note the difference between the application directory and the application storage directory- the app directory cannot be edited, while app storage can.
Use URLLoader to grab a file’s bytes off the intertubes. Then use the filesystem classes to write the bytes to a file in app storage. I can try this out personally if you want.
Here we go. Simple example, takes an mp3 off of Flashkit and sticks it in the application storage directory. This code can go right in the timeline.
import flash.filesystem.*;
var url:String = "http://downloads.flashkit.com/loops/Techno-Dance/Techno/Cheap_Ga-Osnoff-8749/Cheap_Ga-Osnoff-8749_hifi.mp3";
var urlRequest:URLRequest = new URLRequest(url);
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, proceed);
urlLoader.load(urlRequest);
function proceed(event:Event):void {
var bytes:ByteArray = urlLoader.data;
var file:File = File.applicationStorageDirectory.resolvePath("./gotit.mp3");
var filestream:FileStream = new FileStream();
filestream.open(file, FileMode.WRITE);
filestream.writeBytes(bytes);
filestream.close();
}
Thank you very much Rezmason. I was wondering if it could be done this way, but was getting a bit worried about how to actually do it and whether it would work or not, but it’s working a treat! Cheers.