简体   繁体   English

下载完成后触发javascript代码

[英]Fire javascript code when download finishes

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. 我需要更新一对旧的经典ASP页面-一个search.asp页面,该页面提供了一个简单的表单,然后将其发布到results.asp页面。 One of the form options on the search page is a drop down list ( <select ) for the "format". 搜索页面上的表单选项之一是“格式”的下拉列表( <select )。 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. 如果用户选择excel格式,则结果页面仅将Response.ContentType设置为application/vnd.ms-excel并添加content-disposition标头以设置文件名并将其作为附件。 That's it: it's up to excel to then correctly render the html, and it generally does a pretty good job. 就是这样:excel才能正确呈现html,并且通常做得很好。

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. 使用Excel选项的原因是,在这种情况下,用户确实确实希望一次搜索看到多达10,000个项目甚至更多。 They'll use Excel to do some additional analysis on the results. 他们将使用Excel对结果进行一些其他分析。 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. 第一步是在表单提交后在搜索页面上显示一个简单的processing...消息,我可以很容易地做到这一点。 In fact, it's been doing this already for the "HTML" format option. 实际上,它已经针对“ HTML”格式选项进行了此操作。 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. 问题是,在下载Excel文件时,我不知道如何告知下载内容,因此我可以再次隐藏该消息,并且现有实现完全不提供有关下载进度的任何反馈。 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. 如果我可以在下载完成后启动javascript函数,则可以对此进行几乎所有的操作,但是我什至还不能这样做。

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. 从理论上讲,您可以在服务器端执行某些操作,并使用AJAX查询服务器以查看已发送了多少下载内容,但我不知道该怎么做。

hmm, would it be possible to do this via ajax maybe? 嗯,有可能通过ajax做到这一点吗? ie, user selects the format, query is sent via ajax, and the appropriate document is loaded into an iframe on search.asp for example. 即,用户选择格式,通过ajax发送查询,然后将适当的文档加载到search.asp上的iframe中。 you could then pick up the succesful event in your ajax call and appropriately deal with the messages. 然后您可以在ajax调用中获得成功的事件并适当地处理消息。

You could have the file download as an attachment by adding a header, so that the user's browser remains operational. 您可以通过添加标题来将文件下载作为附件下载,以使用户的浏览器保持运行状态。 I realize this isn't exactly what you're asking for; 我意识到这并非您所要的; but if you combine this with the interstitial page you are using for the HTML version, it might be an improvement for user experience. 但是,如果将其与用于HTML版本的插页式页面结合使用,则可能会改善用户体验。

Response.AddHeader "Content-Disposition", "attachment; filename=report.xls"

The user would see the interstitial page with the processing graphic which would then re-post the search form. 用户将看到带有处理图形的插页式页面,然后将其重新发布搜索表单。 This would allow the "Processing..." graphic to display until the page popped up with a download prompt for the Excel file. 这将允许显示“正在处理...”图形,直到页面弹出并带有Excel文件的下载提示。

Update: I tried using an iframe, having the page periodically check the document object and trapping the "interface does not exist" error for when it switches to Excel, but it still locks the browser while the Excel document is downloading in the iframe... I don't think a pure js solution is going to work. 更新:我尝试使用iframe,让页面定期检查文档对象,并在切换到Excel时捕获“接口不存在”错误,但是在iframe中下载Excel文档时,它仍然锁定了浏览器。我认为没有一个纯粹的js解决方案会起作用。 After that I think the next best avenue to pursue is (unfortunately) Flash. 在那之后,我认为下一个最好的途径是(不幸的是)Flash。 Good luck. 祝好运。

I would recommend looking into modifying your upload method to use something like SWFUpload which allows for JavaScript callback on the code. 我建议您考虑修改您的上传方法,以使用SWFUpload之类的方法,该方法允许对代码进行JavaScript回调。 Once the file is uploaded (assuming you are storing it), I would look into passing the filename and type to your Results.asp page where it would then retrieve it form the file store. 文件上载后(假设您正在存储文件),我会考虑将文件名传递给您的Results.asp页,然后在该页中从文件存储中检索文件。 It's not as secure as keeping it in memory which you elude to in your question; 它不像将其保存在问题中那样安全。 however, would provide a better user experience and may provide the callback solution you need. 但是,它将提供更好的用户体验,并可能提供您需要的回调解决方案。

http://swfupload.org/ http://swfupload.org/

Here's a jQuery plug in example that makes the upload process and SWFUpload integration easy: http://blog.codeville.net/2008/11/24/jquery-ajax-uploader-plugin-with-progress-bar/ 这是一个jQuery插件示例,可轻松完成上传过程和SWFUpload集成: http : //blog.codeville.net/2008/11/24/jquery-ajax-uploader-plugin-with-progress-bar/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM