如何显示一个进度条,同时生成一个PDF文件下载,无IE"下载"警告进度条、文件、QUOT、PDF

2023-09-10 19:34:28 作者:后知后觉.

我有客户端和服务器之间的通信。这个过程很简单:

I've got a communication between client and server. The process is simple:

在我做一个AJAX POST请求到一个控制器,它产生于文件系统中的PDF 在上述请求的形式GET请求发送到获取PDF背部和标准浏览器保存或打开成功对话框打开

我有两个步骤的原因是因为我需要有一个进度条正在生成的PDF时。

The reason I'm having a two step process is because I need to have a progress bar while the pdf is being generated.

第二个请求是一种形式的原因提交,而不是AJAX请求的一部分,是因为我无法得到标准的浏览器保存或打开对话框。

The reason of the second request being a form submit rather than part of the AJAX request is because I can't get the standard browser "save or open" dialog box.

使用这种方法的问题是,IE7&安培; IE8弹出一个自己讨厌的通知栏通知的是危险的用户下载的内容,我不希望出现这种情况。

The problem with this approach is that IE7 & IE8 pop up a their annoying notification bar notifying the user that the it is dangerous to download the content and I don't want that.

所以:

我所需要的进度条。 如果我去了一个Ajax请求我需要一种方法来弹出保存或打开对话框。 如果我走了,一个形式GET提交,我需要知道什么时候该文件已经以停止进度条,像服务器和客户端之间的共享标记生成的一种方式。

任何帮助AP preciated。

Any help appreciated.

推荐答案

一个可能的(有点反模式)的解决方法是这样的,但它不是一个伟大的设计可能是

One possible (a little of an anti pattern) workaround is this, but it's not a great design probably

不过,我已经看到了这一点(我承认这样做)面前,除了有些愧疚,但它确实的伎俩

However I've seen this (and admit I did this) before, and except some guilt, it did the trick

用户点击去生成PDF和将只是等待(超时设置应适用),直到它准备的servlet的链接...(内容处理标头等等)就像一个普通的下载

user clicks a link that goes to the servlet that generates the PDF and will just wait (timeout settings should be applied) until it's ready... (Content-Disposition header etc...) just like a regular download

该servlet将报告上的共享会话变量的进步而产生的PDF

The servlet will report progress on a shared session variable while generating the PDF

一个AJAX调用服务器将从会话变量读取进度,并显示给用户

an AJAX call to the server will read from the session variable the progress and show to the user

在PDF完成后,浏览器只是下载它(请求超时的风险也许)

when the PDF is done, the browser will just download it (request timeout risk perhaps)

这里的主要问题是使用请求线程作为工作线程并阻止它,这可能会给这个答案一对夫妇downvotes ...

The main issue here is using the request thread as a worker thread and blocking it, which may give this answer a couple of downvotes...

我没那么熟悉的消息驱动Bean,但这是另外一个,可能是更好的解决方案。

I'm not that familiar with Message Driven Beans but this is another, probably better solution.