Skip to main content
Participant
July 26, 2011
Question

Download files form server on mobile- Flash Builder 4.5

  • July 26, 2011
  • 2 replies
  • 844 views

Hi,

I want to download a simple file from internet or a local tomcat server and use it in my app. I tried  the code below but doesnt seem

to work. Please help.

                var urlRequest:URLRequest = new URLRequest(url);
                urlRequest.method = URLRequestMethod.GET;
                var f:File = new File();
                f.download(urlRequest,"wallpaper.jpg");
                f.addEventListener(Event.COMPLETE, completeHandler);

Thanks

Monika

This topic has been closed for replies.

2 replies

July 28, 2011

This code works for my on Android app (adapt to you own purposes):

protected var myFile:File;

//using a button to manually download a file

protected function buttonDownload_clickHandler(event:MouseEvent):void

{

var fileUrl:String = "http://path.to.my.file";

var fileName:String = fileUrl.substr(fileUrl.lastIndexOf('/')+1);

myFile = storageDir.resolvePath(fileName);

if (myFile.exists){

//do something with the file

} else {

loadFile(fileUrl);

}

}

protected function loadFile(fileUrl:String):void

{

if (!myFile.exists)

{

trace("View Activate - local file does not exist");

spinner.visible = true;

lbl.text = "Downloading file...";

urlLoader.dataFormat = URLLoaderDataFormat.BINARY;

urlLoader.load(new URLRequest(fileUrl));

urlLoader.addEventListener(Event.COMPLETE,onFileLoaded);

}

else

{

//do something with the file

}

}

// Save the image to the SD card for faster access next time

protected function onFileLoaded(event:Event):void

{

var fileStream:FileStream = new FileStream();

fileStream.open(myFile, FileMode.WRITE);

fileStream.writeBytes(urlLoader.data);

fileStream.close();

spinner.visible = false;

lbl.text="File: "+ myFile.nativePath + " downloaded.";

urlLoader.removeEventListener(Event.COMPLETE,onFileLoaded);

//do something with the file if you want

}

July 26, 2011

The File.download opens up a native operating-system dialog box before it saves the file and the application can use it, and hence you cannot use this if you are developing for mobile platform.

You can use FileStream API, in case you wish to only access the contents of the remote file.

Please share your use case further, if you face any issues.