如何在flex 4或flex 4.5中从服务器下载文件而无需用户交互?

时间:2021-07-20 03:48:43

The code flow is as follows:

代码流程如下:

user clicks 'download details' button on the page -> on button click handler a call to a RPC method is made using s:CallResponder -> RPC method generates and returns a URL from where to download the file -> the success event handler of the CallResponder gets the url where FileRefrence.download() is used to download the file but throws following error:-

用户单击页面上的“下载详细信息”按钮 - >按钮单击处理程序使用s调用RPC方法:CallResponder - > RPC方法生成并返回从哪里下载文件的URL - >成功事件处理程序CallResponder获取url,其中FileRefrence.download()用于下载文件,但抛出以下错误: -

Error: Error #2176: Certain actions, such as those that display a pop-up window, may only be invoked upon user interaction, for example by a mouse click or button press. at flash.net::FileReference/download()

错误:错误#2176:某些操作(例如显示弹出窗口的操作)可能仅在用户交互时调用,例如通过鼠标单击或按下按钮。在flash.net::FileReference/download()

Code is follows:

代码如下:

<fx:Script>
    <![CDATA[            

        import mx.rpc.CallResponder;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;

        protected function downloadButtonClickHandler(event:MouseEvent):void 
        { 
                var web_service:IWeb_service = Web_service.getInstance(); 
                getDetails.token = web_service.getURLDetails();
        } 

        public function onGetDetailsResult(event:ResultEvent):void
        {

            var response:URLResponse = event.result as URLResponse;
            if(response != null && response.url != null)
            { 
                var request:URLRequest = new URLRequest(response.url);
                fileReference.download(request, "test.html");
            }
        }

        public function onGetDetailsFault(event:FaultEvent):void
        {
            Alert.show("Error in downloading details");
        }
    ]]>    
</fx:Script>

<fx:Declarations>  
    <net:FileReference id="fileReference" />
    <s:CallResponder id="getDetails" result="onGetDetailsResult(event)" fault="onGetDetailsFault(event)"/>

</fx:Declarations>
<s:HGroup width="100%" height="100%">

       <s:Button label="Download Details" id="downloadButton" 
                          click="downloadButtonClickHandler(event)" /> 
</s:HGroup>

Is there any other way of achieving this as File FileReference needs a user interaction which is not happening int his case?

有没有其他方法可以实现这一点,因为File FileReference需要一个用户交互,而这种情况在他的情况下没有发生?

3 个解决方案

#1


1  

I don't think there is a way to download the file via FileReference, due to security restriction, if you don't have the file available beforehand.

由于安全限制,如果您事先没有可用文件,我认为没有办法通过FileReference下载文件。

As far as I'm aware there are two options to handle this scenario:

据我所知,有两种方法可以处理这种情况:

  1. Trigger FileReference download via separate button click (e.g. Alert dialog);
  2. 通过单独的按钮单击(例如,警报对话框)触发FileReference下载;
  3. Initiate browser's standard download via navigateToURL(myURLRequest, "_self");
  4. 通过navigateToURL(myURLRequest,“_ self”)启动浏览器的标准下载;

Hope this helps.

希望这可以帮助。

#2


0  

var url:String = "test.xls";

var fileReference:FileReference = new FileReference; 
var urlRequest:URLRequest = new URLRequest(url);
navigateToURL(urlRequest,"_new");

fileReference.download(urlRequest);

#3


0  

Invoke web_service.getURLDetails() before the user clicks on download button (for example after the flex application is created). You can check in the downloadButtonClickHandler method if the url was loaded, or you can start with a disabled button and change the status to enabled after the url was loaded.

在用户单击下载按钮之前调用web_service.getURLDetails()(例如,在创建Flex应用程序之后)。如果加载了url,您可以检查downloadButtonClickHandler方法,或者可以在禁用加载按钮后启动禁用按钮并将状态更改为启用。

#1


1  

I don't think there is a way to download the file via FileReference, due to security restriction, if you don't have the file available beforehand.

由于安全限制,如果您事先没有可用文件,我认为没有办法通过FileReference下载文件。

As far as I'm aware there are two options to handle this scenario:

据我所知,有两种方法可以处理这种情况:

  1. Trigger FileReference download via separate button click (e.g. Alert dialog);
  2. 通过单独的按钮单击(例如,警报对话框)触发FileReference下载;
  3. Initiate browser's standard download via navigateToURL(myURLRequest, "_self");
  4. 通过navigateToURL(myURLRequest,“_ self”)启动浏览器的标准下载;

Hope this helps.

希望这可以帮助。

#2


0  

var url:String = "test.xls";

var fileReference:FileReference = new FileReference; 
var urlRequest:URLRequest = new URLRequest(url);
navigateToURL(urlRequest,"_new");

fileReference.download(urlRequest);

#3


0  

Invoke web_service.getURLDetails() before the user clicks on download button (for example after the flex application is created). You can check in the downloadButtonClickHandler method if the url was loaded, or you can start with a disabled button and change the status to enabled after the url was loaded.

在用户单击下载按钮之前调用web_service.getURLDetails()(例如,在创建Flex应用程序之后)。如果加载了url,您可以检查downloadButtonClickHandler方法,或者可以在禁用加载按钮后启动禁用按钮并将状态更改为启用。