0

Hi now I have found how to give a relative path in URLRequest paramate and download that file. I found it from this particular stack overflow post . Thanks to Christian Nunciato and heri0n.

so now if If give my machine's relative path, C:/sample/DefectList.xls it works. Now I have to access an xls file kept in the server machine or any other machine,say my team mate's machine. The ip address is 172.17.196.124 and the location is C:/sample/test.xls.

I tried
var request:URLRequest = new URLRequest"file://172.17.196.124/c:/sample/test.xls"); But it throws the Error#2032.

How to mention a remote location as a relative path?

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="loadFile()"> 

<mx:Script> 
    <![CDATA[ 

            private var loadedFile:ByteArray; 

            private function loadFile():void 
            { 
                    //var request:URLRequest = new URLRequest("C:/sample/DefectList.xls"); 
                    var request:URLRequest = new URLRequest("file://172.17.196.124/c:/sample/test.xls"); 

                    var urlLoader:URLLoader = new URLLoader(request); 

                    urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete); 
                    urlLoader.dataFormat = URLLoaderDataFormat.BINARY; 
                    urlLoader.load(request); 
            } 

            private function onURLLoaderComplete(event:Event):void 
            { 
                    loadedFile = event.target.data; 
            } 

            private function saveLoadedFile():void 
            { 
                    var file:FileReference = new FileReference(); 
                    file.save(loadedFile);  
            } 

    ]]> 
  </mx:Script> 

  <mx:Button label="Save File" horizontalCenter="0" verticalCenter="0" click="saveLoadedFile()" /> 

</mx:Application> 
Community
  • 1
  • 1
Angeline
  • 2,369
  • 22
  • 68
  • 107

2 Answers2

0

You cannot download something directly from flex onto the user's desktop. You have to do it from the server. To initiate downloading a file from the server, use FileReference.download() method. The downloading will happen only with the user's consent - if the user clicks cancel on the download prompt window, it will be canceled.

downloadURL = new URLRequest();
downloadURL.url = filename_relative_to_swf_location;
file = new FileReference();
file.addEventListener(Event.COMPLETE, completeHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
file.download(downloadURL, fileName);
private function completeHandler(event:Event):void 
{
  trace("completeHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void 
{
  trace("ioErrorHandler: " + event);
}
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
  • well, my problem is that if I need to use URLRequest method, I must use http or https for url. But I have the file to be downloaded from a local file system, whish has only relative path, like C:/Documents/... etc. So i cant use URLRequest. If I use URLLoader,I can get the contents by giving relative path in URLRequest. Now I need to save the contents as a file. I have edited my question to hold a part of my code – Angeline Nov 05 '09 at 08:53
  • Let me get this straight : You wanna load a file from user's desktop and download it back to the user (probably after modifying)? – Amarghosh Nov 05 '09 at 09:10
  • No actually I want to download the file from a server machine to my local machine. But I would get only the relative path of the file location in the server. I have edited my question in a better way.please see that.. – Angeline Nov 05 '09 at 10:35
  • You cannot load a file from a server using addresses like "D:/urlLoaderExample.txt" - this is a local file at client, not a server side file. Server side files would have either relative path (like assets/data.txt) or full path (like http://domain.com/assets/data.txt). – Amarghosh Nov 05 '09 at 10:50
  • For now we dont have any domain name for the server. We just use the IP address. So how can I do it in this case? – Angeline Nov 05 '09 at 11:10
  • I don't think that `file://` protocol can access other machines. Only way I can think of is to set up a webserver in that machine, copy the file to its http root folder and access `http://172.17.196.124:8080/file.txt` – Amarghosh Nov 05 '09 at 11:24
0

If you're trying to create an application that will work with local files you should probably create an AIR project rather than a web Flex one, as it gives you tools to work with files. The local files will only work if you open the compiled swf from your hard-drive. The security policy will forbid any flex swf from easily accessing your local files if the swf is loaded from a web server.

A few videos that might come in handy:

* Comparing Flash, Flex, Flash Player and Adobe AIR (5:30)
* Exercise 13: Creating and deploying an AIR application

on Flex in a Week

Robert Bak
  • 4,246
  • 19
  • 20