Hi, I use servlet create some link on my page to let user download PDF document. I use "<A href=http://192.168.../../../myPDF.pdf>" create link for download. But the browser wouldn't open a download dialog window like ".zip", it will open PDF file directly in my browser. How can I create a link let user to download PDF file? Thanks.
The browser decides what to do with links. If the browser is configured to recognize *.pdf files, it will perform the default operation (in some browsers, this is user specified). In most cases that means "launch the program that uses this type of file".
Originally posted by Mike Curwen: The browser decides what to do with links.
Well, yes, but that doesn't mean you have no say in it. The behaviour is governed by the Mime headers that the web server sends back with the file. First,the Content-Type header. If the content type is one the browser doesn't have a handler for, it displays the "Save As" dialog. If desperate, you can always configure the web server, servlet or JSP to use some absolutely wild Mime type Second, the Content-Disposition header. Unfortunately, this is where I'm getting out of my depth (alright, I'm just too lazy to start digging through books and specs). I think, but am not sure, that if you specify "attachment; filename=filename" then the browser will display the "Save As" dialog even if it has a handler or plug-in for the type. Take my words here with a grain of salt though. - Peter
Joined: Oct 04, 2001
Thanks Mike and Peter. Right now I understand why my browser always open those PDF files. Then here comes another question: How to make browser can not reconize PDF file and create a save as log?
I would go with Peter's suggestion. There is really no way for you to code an HTML page that makes a browser ignorant of it's own settings. Users would probably not like you very much if you even *could* do this sort of thing.
But when Peter posted his reply, I knew that was the right track, because I do recall there are websites where (without right-clicking), the two links are: "read the pdf" and "download the pdf".
So there must be a way, and Peter is starting you on the right direction I think.
Now THIS is interesting! About the content-disposition header - depending on your browser brand and version (also, whether you have a buggy release of IE), the content-disposition can be used to indicate whether a file should be displayed, downloaded, or otherwise disposed of. So if you want a PDF to be saved instead of displayed, set the appropriate Content-Disposition and hope the user's browser wants to co-operate. I should point out that an httpd download is NOT a named file! The way the user's browser determines what to do with content by default (no Content-Disposition) is in no way determined by the C.D. "file name", since that's just a hint to the browser for the name displayed in the "Save as" dialog and can be overridden - for example, they can change "10K.PDF" to "CRUD.TXT" and that's the name the document will store under on the user's machine (when Notepad doesn't display it properly, that's THEIR fault!). In the case of an embedded link on an HTML page to a graphic - or a PDF, the web server looks at the filename extension on the link's URL to get its MIME type, for generated content (dynamic PDF), YOU set the MIME type. The MIME type is what counts, not the filename. Only AFTER the file resides on the client's machine and is being accessed by the client OS does the filename extension become important (most OS's have nowhere to store the file's original MIME type anyway!). That probably wasn't as clear or as succint as I'd hoped - but maybe it will help...
Customer surveys are for companies who didn't pay proper attention to begin with.
Anyway, the above is all theory. For the original question, the answer would be to replace the HREF to the PDF with the URL of a servlet that copies the actual PDF out with its own special headers (Content-disposition and maybe a binary-download MIME).
Joined: Oct 04, 2001
Hi, Tim: Thanks for your help.I just want to make it clear. Do you mean I need a special servlet for downloading PDF file, and this servlet will read PDF first then pass it with MIME header?