How to specify the location of a remote machine's file in the URLRequest parameter? - Edited
Hi now I have found how to give a relative path in URLRequest para开发者_StackOverflow社区mate 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>
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);
}
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
精彩评论