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 an error screen. Downloading Google Chrome works in a similar manner:
.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:
- 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
- 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:
- 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
- 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)
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 an error screen. Downloading Google Chrome works in a similar manner:
http://www.google./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:
- 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
- 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:
- 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
- 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)
- You lost me at "fetching PDF's two times"... why would you ever have to fetch it twice? – Dolph Commented Sep 8, 2011 at 14:39
- Hmm, true, that might not make sense outside of my head. I'll rephrase that section – Lukas Eder Commented Sep 8, 2011 at 14:45
- You can display pdf files with javascript. The most popular library is pdf.js, demo. Also you can fetch raw html via ajax request (or part of code on the page) and generate pdf with jspdf as binary data via data:uri - demos – atma Commented Sep 8, 2011 at 15:04
- @atma: Amazing libraries! They might not be useful for me, but nice to see... – Lukas Eder Commented Sep 8, 2011 at 15:18
-
Just interesting. If you want to use the browser's default viewer and only one request, why you can not encode on the server side pdf file (e.g.
base64_encode
in php) and make response like a{success: true, message: "all ok", code: xx, data: encoded_bin_data}
. Thenif (resp.success) document.location.href = 'data:application/pdf;base64,'+encoded_bin_data; else show_error_page(resp.code)
? – atma Commented Sep 8, 2011 at 15:51
1 Answer
Reset to default 3The implemented "old-school" solution works like this:
- The JavaScript client sends an AJAX request to the server to "prepare" a PDF document
- The server responds with any of these three messages:
- a) Document available at URL http://www.example./doc.pdf
- b) Document unavailable
- c) Document being "prepared" (i.e. client has to wait)
- 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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745299212a4621331.html
评论列表(0条)