• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to create a link to download pdf file?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just instruct the user of your page to RIGHT CLICK on the link and choose "Save Target As..." (IE).
In Netscape it's "Save Link As..."
 
Jim Wang
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony. Is that possible to let user just click one link on my page to download PDF file to local machine?
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,

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

You may want to investigate the use of funky javascript tricks.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jim Wang
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Jim Wang
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This link to an article written sometime back forces the browser not to interpret the attachment but download it.
http://www.planet-source-code.com/xq/ASP/txtCodeId.2183/lngWId.2/qx/vb/scripts/ShowCode.htm
Hope this helps.
cheers!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic