How to show a progress bar while generating a PDF file for download, without IE “download” warnings

做~自己de王妃 提交于 2019-12-23 13:05:21

问题


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

  1. I'm making an AJAX POST request to a controller which generates a pdf in the file system
  2. On success of the above request a form GET request is made to get the pdf back and the standard browser "save or open" dialog box opens up

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

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.

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.

So:

  1. I need the progress bar.
  2. If I go for an AJAX request only I need a way to pop up the "save or open" dialog box.
  3. If I go for the a form GET submit I need a way of knowing when the file has been generated in order to stop the progress bar, something like a shared flag between server and client.

Any help appreciated.


回答1:


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

  • 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

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

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

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

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...

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



来源:https://stackoverflow.com/questions/10536425/how-to-show-a-progress-bar-while-generating-a-pdf-file-for-download-without-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!