下载完成时启动JavaScript代码 [英] Fire javascript code when download finishes

查看:85
本文介绍了下载完成时启动JavaScript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更新一对旧的经典asp页面—一个 search.asp 页面,它提供了一个简单的表单,然后发布到 results.asp 页面。搜索页面上的一个表单选项是格式的下拉列表(< select )。如果用户选择excel格式,结果页面只需将Response.ContentType设置为 application / vnd.ms-excel ,并添加一个content-disposition标题以设置文件名称和让它成为一个附件。就是这样:它能够正确渲染html,并且通常做得非常好。



除了一件事外,所有这些工作都很好。 Excel选项的原因在于,在这种情况下,用户确实希望为单个搜索查看多达10,000个项目甚至更多。他们将使用Excel对结果进行一些额外的分析。所以搜索操作通常需要一分多钟,我无法改变这一点。

那一分钟的用户体验不太理想。用户不仅仅坐在那里几乎没有任何反馈,但通常有足够的结果让页面溢出响应缓冲区。这意味着页面必须定期刷新,因此文件立即开始下载,但下载管理器本身无法提供有意义的反馈。我的任务是改善这种情况。



第一步是只显示一个简单的处理... 消息在表单提交时在搜索页上,我可以很容易地做到这一点。事实上,它已经为HTML格式选项做了这个。问题是,当下载Excel文件时,我不知道如何告诉下载的任何内容,所以我可以再次隐藏该消息,现有的实现根本没有提供任何关于下载进度的反馈。有任何想法吗?如果我可以在下载完成时触发一个JavaScript函数,那么我可以将任何东西都挂钩,但我甚至无法做到这一点。



更新:

我重新提出了这个问题,试图更清楚地表明问题。

>据我所知,浏览器并没有为您提供任何下载已经进展的钩子。理论上,你可以在服务器端做一些事情,并使用AJAX来查询服务器,看看有多少下载被发送,但我不知道该怎么做。


I need to update a pair of old classic asp pages— a search.asp page that provides a simple form which is then posted to a results.asp page. One of the form options on the search page is a drop down list (<select) for the "format". If the user chooses the excel format the results page just sets the Response.ContentType to application/vnd.ms-excel and adds a content-disposition header to set the file name and make it an attachment. That's it: it's up to excel to then correctly render the html, and it generally does a pretty good job.

All that works pretty well, except for one thing. The reason for the Excel option is that in this case the users really do want to see as many as 10,000 items or even more for a single search. They'll use Excel to do some additional analysis on the results. So the search operation typically takes just over a minute and I can't change that.

The user experience during that minute is less than ideal. Not only is the user just sitting there with little to no feedback, but there are often enough results that the page overflows the response buffer. This means the page has to flush periodically, and therefore the file starts downloading right away but the download manager isn't able to provide meaningful feedback by itself. My mission is to improve the situation.

The first step is to just show a simple processing... message on the search page when the form submits, and I can do that easily enough. In fact, it's been doing this already for the "HTML" format option. The problem is that when downloading the Excel file I don't know how to tell anything about the download so I can hide the message again, and the existing implementation doesn't provide any feedback on download progress at all. Any ideas? If I can just get a javascript function to fire when the download completes I can hook just about anything to that, but I can't even do that yet.

Update:
I re-worded the question to try to present the problem more clearly.

解决方案

As far as I know, browsers don't offer you any hooks as to how far a download has progressed. In theory, you could do something on the server side and use AJAX to query the server to see how much of the download has been sent, but I don't know how to do it.

这篇关于下载完成时启动JavaScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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