如何在 URLRequest 参数中指定远程计算机文件的位置? - 编辑

发布于 2024-08-10 17:44:48 字数 1887 浏览 5 评论 0原文

您好,现在我已经找到了如何在 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 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> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抹茶夏天i‖ 2024-08-17 17:44:48

您无法直接从 Flex 下载某些内容到用户的桌面上。您必须从服务器执行此操作。要开始从服务器下载文件,请使用 FileReference.download() 方法。只有在用户同意的情况下才会进行下载 - 如果用户在下载提示窗口中单击“取消”,下载就会被取消。

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);
}

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);
}
旧城烟雨 2024-08-17 17:44:48

如果您尝试创建一个可以处理本地文件的应用程序,您可能应该创建一个 AIR 项目而不是 Web Flex 项目,因为它为您提供了处理文件的工具。仅当您从硬盘打开已编译的 swf 时,本地文件才有效。如果 swf 是从 Web 服务器加载的,则安全策略将禁止任何 Flex swf 轻松访问您的本地文件。

一些可能会派上用场的视频:

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

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:

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

on Flex in a Week

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文