Flex 的 FileReference.save() 只能在用户事件处理程序中调用——我该如何解决这个问题? [英] Flex's FileReference.save() can only be called in a user event handler -- how can I get around this?

查看:13
本文介绍了Flex 的 FileReference.save() 只能在用户事件处理程序中调用——我该如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Web 服务调用完成后调用 FileReference.save(),但此方法有一个限制:在 Flash Player 中,您只能成功调用此方法以响应用户事件(例如,在鼠标单击或按键事件的事件处理程序).否则,调用此方法会导致 Flash Player 抛出错误异常."(来自文档 这里)

I need to call FileReference.save() after a web service call has completed, but this method has a restriction: "In Flash Player, you can only call this method successfully in response to a user event (for example, in an event handler for a mouse click or keypress event). Otherwise, calling this method results in Flash Player throwing an Error exception." (from the documentation here)

这个限制有点模糊.这是否意味着我只能从注册为某些类型用户事件的侦听器的事件处理函数中调用 FileReference.save() 方法?如果是这样,那么究竟哪些用户事件是有效的?(也许有一个事件永远不会被用户与我的应用程序交互调度,我可以为该事件类型注册一个事件处理程序函数并从该函数内调用 save() ?)

This restriction is a bit vague. Does it mean that I can only call the FileReference.save() method from within an event handler function that is registered as a listener for certain types of user events? If so then exactly which user events are valid? (Perhaps there's an event that will never be dispatched by user interaction with my application and I could register an event handler function for that event type and make the save() call from within that function?)

我的困难在于,在我的 Web 服务返回将用作 FileReference.save() 方法调用的参数的数据之前,我无法安全地调用 FileReference.save() 方法,因此触发的事件FileReference.save() 调用实际上是一个 ResultEvent 而不是用户事件,而且我很担心调度一个新的(虚假的)用户事件类型以便能够触发 FileReference.save() 调用,除非它绝对是一个永远不会因用户与我的应用程序的实际交互而分派的用户事件.

My difficulty is that I can't safely call the FileReference.save() method until my web service returns with the data that will be used as the argument of the FileReference.save() method call, so the event that triggers the FileReference.save() call is actually a ResultEvent rather than a user event, and I'm leery of dispatching a new (faux) user event type in order to be able to trigger the FileReference.save() call unless it's definitely a user event that would never be dispatched as a result of actual user interaction with my application.

简而言之,我现在正在做的是:我有一个函数,它被注册为按钮单击的处理程序.在此函数中,我调用 Web 服务以从服务器获取数据.我还有一个结果处理函数,它在 Web 服务调用完成时被调用,在这里我想调用 FileReference.save() 方法,因为此时我知道数据已准备好保存到一份文件.但上述限制阻止我这样做 - 我收到一个错误:

In a nutshell what I'm doing now is this: I have a function that is registered as a handler for a button click. In this function I make my web service call to fetch data from the server. I also have a result handler function which gets invoked when the web service call completes, and it's in here that I want to call the FileReference.save() method since it's at this point that I know that the data is ready to be saved to a file. But the aforementioned restriction is blocking me from doing this -- I get an 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.

我尝试了很多方法来解决这个问题,例如使用 FileReference.save() 调用创建第二个鼠标单击事件处理函数,并在超时间隔后调用它(以使 Web 服务有时间完成),但我一直遇到同样的错误——也许这种方法不起作用,因为第二个函数没有注册为用作其参数的事件类型的事件侦听器.

I've tried many things to get around this such as creating a second mouse click event handler function with the FileReference.save() call within and calling it after a timeout interval (to give the web service time to complete), but I keep running into the same error -- maybe that approach doesn't work since the second function isn't registered as an event listener for the event type used as its argument.

我是 Flex 开发的新手,所以也许我只是没有以正确的方式思考这个问题.如果有人可以建议另一种方法,我会非常感激.预先感谢您的意见或建议.

I'm new to Flex development so perhaps I'm just not thinking about this in the right way. If anyone can suggest another approach I'd really appreciate it. Thanks in advance for your comments or suggestions.

--詹姆斯

推荐答案

Adobe 这样做是作为一种安全措施,以确保用户是那些弄乱文件的人,而不是潜在的有害代码.我的理解是,他们通过仅允许源自 UI 组件的(单击?)事件的处理程序执行 FileReference 方法来强制执行此操作,因此以编程方式生成您自己的事件将不起作用,尽管我没有尝试验证这一点.不幸的是,我发现的最佳解决方案是稍微重新设计 UI 以符合此约束.在您的特定情况下,您可以使用一个类似准备下载"的按钮将其设置为两次单击过程,该按钮在 Web 服务完成后更改为下载文件".从用户的角度来看,这不太理想,但我认为没有什么可以做的,除非您可以在显示触发 FileReference.save() 调用的按钮之前以某种方式完成 Web 服务调用.

Adobe does this as a sort of security measure to ensure users are the ones messing with files rather than potentially harmful code. My understanding is that they enforce this by only allowing handlers of (click?) events that originate from UI components to execute the FileReference methods, so generating your own events programmatically will not work, although I have not tried to verify this. Unfortunately the best resolution I've found is to re-work the UI a bit to conform to this constraint. In your particular situation, you could make this a two click process with a button that says something like "Prepare Download", which changes to "Download File" after the web service is complete. This is less than ideal from a user perspective, but I don't think there's much else that can be done unless you can somehow complete your web service call prior to displaying the button that triggers the FileReference.save() call.

这篇关于Flex 的 FileReference.save() 只能在用户事件处理程序中调用——我该如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆