开发者

AJAX PDF download with jquery

开发者 https://www.devze.com 2023-04-03 08:29 出处:网络
This is what I want to do: I want to send an HTTP request to a server, potentially returning a PDF file. But the server may also just return an error code (PDF file unavailable, PDF file invalid, PDF

This is what I want to do:

I want to send an HTTP request to a server, potentially returning a PDF file. But the server may also just return an error code (PDF file unavailable, PDF file invalid, PDF system down, etc). When I get the PDF, I would like to open the PDF and refresh the page that loaded the PDF, because the PDF is then marked as "read". When I get an error code (or timeout), I would like to redirect the page to开发者_Python百科 an error screen. Downloading Google Chrome works in a similar manner:

http://www.google.com/chrome/eula.html?hl=en&platform=win

This is what I don't want do:

For performance reasons, I don't want to issue two requests as suggested in this question here:

Download and open pdf file using Ajax

Two requests can mean:

  1. Make a request for the PDF and return a code to indicate whether the PDF is available or not. If unavailable, immediately display an error page
  2. If it is available, open a window and request the PDF again in that window, and display it.

That's expensive because the PDF's have to be accessed via remote systems. I don't want to access the PDF resource twice. Another solution involving two requests:

  1. Make a request for the PDF and retrieve an error code or a temporary URL where the PDF is cached. On error, immediately display an error page
  2. If the PDF is available, open a window in which the cached PDF is displayed.

This will require for quite a large cache for the PDF's

This might be an interesting lead:

I found this question here giving me some information about how I could download the binary data and make it available in JavaScript as binary data:

Is there a way to read binary data in JavaScript?

Maybe that's a nice lead, but of course it won't solve my problem yet, as I want to use the browser's default editor to open the file, just as if I had requested the file from a normal URL.

So the question is:

Can I download binary data and open them like a regular document from JavaScript? If not, I'll cache the document in some managed memory container in Weblogic and just hope that this won't kill our system. Please only respond:

  • If you know for sure it cannot be done (some links explaining why would be nice)
  • If you know how to do it
  • If you have a different solution doing roughly what I want to do (not issuing two requests)


The implemented "old-school" solution works like this:

  1. The JavaScript client sends an AJAX request to the server to "prepare" a PDF document
  2. The server responds with any of these three messages:
    • a) Document available at URL http://www.example.com/doc.pdf
    • b) Document unavailable
    • c) Document being "prepared" (i.e. client has to wait)
  3. The JavaScript client then reacts as such:
    • a) Open the returned URL in a new window, refresh the current window after 5 seconds
    • b) The current window is redirected to an error screen
    • c) The current window stays unchanged and AJAX polling is implemented to repeat step 2
0

精彩评论

暂无评论...
验证码 换一张
取 消