We help IT Professionals succeed at work.

How to download a file and find path in Flex

StarDusterII
StarDusterII asked
on
I'm using Flash Builder 4.5.1 to create a mobile app and I want to download a file and then know where it was stored.  I tried fileRef and can get the file name but can't figure out how find the path where it's located.  I used:

var urlRequest:URLRequest = new URLRequest(webServer/videoFile);
fileRef = new FileReference();
fileRef.download(urlRequest, videoFile);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
...
private function completeHandler(event:Event):void{
var s:String = event.currentTarget.name;  // Just the name of the file

Comment
Watch Question

Top Expert 2011

Commented:
Ok, how we can help you. We don't know you server location and file structure.?

Author

Commented:
Sorry, I didn't think it would matter.  The server is my web server and the files will be an appropriate video file for the platform that it's downloaded to so it can be played when the device is off-line.
I was able to use the following to download the file and get the path.

file = File.applicationStorageDirectory;
file.download(urlRequest, videoFile);
file.addEventListener(Event.COMPLETE, completeHandler);
...
private function completeHandler(event:Event):void{
videoFile = event.currentTarget.url;               <--  Either of these willl work
videoFile = event.currentTarget.nativePath;  <--

Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for StarDusterII's comment http:/Q_27476161.html#37231575

for the following reason:

No other solutions were provided.
Top Expert 2011

Commented:
The code what you provided will work only for AIR applications not for web

Author

Commented:
Right.  As it says in the original post, I'm creating a mobile app with Flash Builder 4.5.1 and by definition that is only an AIR application.  Your post provided no value and this solution works exactly as needed.
Top Expert 2011

Commented:
So, what do you want from expert your code should work on mobile apps?
I think the download will be made to some temporary location. Even if it might result somewhere in the filesystem, I think the flash player will abstract from this. If you want to save such a file in the normal storage and to have it have a path and a file name, you will propably have to make the user decide by displaying a File-Chooser popup. Since FlashPlayer 10 there is no longer a way to specify the complete location of a download without explicit user-interaction. This is a security measure that was added in FlashPlayer 10.

Author

Commented:
The code I posted will display the file chooser and allow the user to put it anywhere they want (which is desired).  When the file is written and the complete handler event fired, whatever path they put it in is in event.currentTarget.url and nativePath.  However, I've heard that nativePath is always blank for Android but haven't tested that yet.
Hmmm ... well in that case ... I'd simply test it ;-)

Author

Commented:
I have and it works well... but that seems like a thousand problems ago now!  ;-)
Restarting the closure procedure.

ModernMatt
Experts Exchange Moderator