如何在 URLRequest 参数中指定远程计算机文件的位置? - 编辑
您好,现在我已经找到了如何在 URLRequest 参数中给出相对路径并下载该文件。我从 这个特定的堆栈溢出帖子 < /a>.感谢 Christian Nunciato 和 heri0n。
所以现在如果给我机器的相对路径,C:/sample/DefectList.xls
它就可以工作。 现在我必须访问保存在服务器计算机或任何其他计算机(例如我队友的计算机)中的 xls 文件。 IP 地址为 172.17.196.124
,位置为 C:/sample/test.xls
。
我试过var request:URLRequest = new URLRequest"file://172.17.196.124/c:/sample/test.xls");
但它抛出错误#2032。
如何将远程位置称为相对路径?
<?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>
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 triedvar 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法直接从 Flex 下载某些内容到用户的桌面上。您必须从服务器执行此操作。要开始从服务器下载文件,请使用 FileReference.download() 方法。只有在用户同意的情况下才会进行下载 - 如果用户在下载提示窗口中单击“取消”,下载就会被取消。
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.
如果您尝试创建一个可以处理本地文件的应用程序,您可能应该创建一个 AIR 项目而不是 Web Flex 项目,因为它为您提供了处理文件的工具。仅当您从硬盘打开已编译的 swf 时,本地文件才有效。如果 swf 是从 Web 服务器加载的,则安全策略将禁止任何 Flex swf 轻松访问您的本地文件。
一些可能会派上用场的视频:
Flex in a Week
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:
on Flex in a Week