处理大文件时的 jQuery throbber

发布于 2024-10-08 01:02:31 字数 128 浏览 0 评论 0原文

在我的 Web 应用程序上,用户可以生成一个 CVS 文件,有时该文件可能会达到 10+ Mb。该报告显然需要一些时间才能生成。我想在生成报告时为用户显示一个 throbber,一旦提示他们保存/运行,我希望隐藏 throbber。这可能吗?

On my web app the user can generate a CVS file that can get pretty large 10+ Mb sometimes. The report obviously can take some time to generate. I want to display a throbber for the user while the report is being generated, once they are prompted to save/run I want the throbber to hide. Is this possible?

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

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

发布评论

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

评论(3

情域 2024-10-15 01:02:31

不,实际上不可能检测文件何时到达以及用户何时保存它。您几乎陷入困境,只是更新您的 throbber 并在他们准备好继续时提供继续链接。

如果这是可能的,那么当您完成文件的登陆页面时,下载网站将使用它自动将您转发到下载列表。

No, it's not really possible to detect when the file arrives and the user saves it. You're pretty much stuck just updating your throbber and offering a continue link when they're ready to continue.

If this was possible then the download sites would use it to auto-forward you to the download list when you're finished at the file's landing page.

檐上三寸雪 2024-10-15 01:02:31

是的,JavaScript 中的繁重操作通常会被分成多个块,然后从 setInterval 中调用。超时可以防止页面冻结。

var csvTxt = "";
var isDone = false;
addPart = function(){
    csvTxt += addTextFromLongCalculation();
    if(csvTxt > 10000000) isDone = true; // this is an arbitrary example
}

var handle = setInterval(function(){
   addPart();
   if(isDone){
        clearInterval(handle);
   }
}, 20);

Yes, typically heavy operations in JavaScript are split into chunks and then called from a setInterval. The timeout keeps the page from freezing up.

var csvTxt = "";
var isDone = false;
addPart = function(){
    csvTxt += addTextFromLongCalculation();
    if(csvTxt > 10000000) isDone = true; // this is an arbitrary example
}

var handle = setInterval(function(){
   addPart();
   if(isDone){
        clearInterval(handle);
   }
}, 20);
鹿港巷口少年归 2024-10-15 01:02:31

我猜您正在发布一些数据,在服务器上生成 CSV,设置内容类型并等待浏览器的保存对话框出现。正确的?

那样的话,我想你会失望的。我花了相当多的时间试图找到适合这种情况的事件,但我无法弄清楚。我最终不得不做一些复杂的事情,比如使用 XHR 来轮询文件创建的状态。一旦得到我想要的响应,我就隐藏了 throbber 并请求 CSV。

更清楚地说:

  • 显示 throbber
  • 使用 XHR 告诉服务器开始 CSV 生成
  • 使用 XHR 轮询 CSV 创建的状态
  • 文件创建完成后:
    • 向文档添加一个不可见的 iframe,该 iframe 指向新创建的 CSV,并让服务器向其中添加内容处置标头。
    • 在短暂延迟后隐藏 throbber(您可以尝试计时,以便在显示文件保存窗口后、用户与窗口交互时隐藏 throbber)。您还可以检测窗口模糊事件来隐藏颤动,但我很确定这不是很可靠。

I'm guessing that you are posting some data, generating the CSV on the server, setting the content-type and waiting for the browser's save dialog to appear. Correct?

In that case, I think you will be disappointed. I spend quite some time trying to find events that would fire for this exact scenario and I couldn't figure it out. I finally had to do something convoluted like use XHR to poll the status of the file creation. Once I got the response I wanted, I hid the throbber and requested the CSV.

To be clearer:

  • Show the throbber
  • Use XHR to tell the server to start the CSV generation
  • Use XHR to poll the status of the CSV creation
  • Once file creation is complete:
    • add an invisible iframe to the document that points to the newly created CSV and have the server add the content-disposition header to it.
    • Hide the throbber after a short delay (you can try to time it so that the throbber is hidden after the file save window is shown, but while the user is interacting with the window). You could also possibly detect the window blur event for hiding the throbber, but I'm pretty sure that won't be very reliable.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文