0

My Java servlet includes the following line:

response.addHeader("Content-Disposition", "filename=myFile.pdf");

I need to include a named destination defined in the PDF file as part of the file name. Ideally, I could use the following:

response.addHeader("Content-Disposition", "filename=myFile.pdf#Chapter3");

but when I run it, the url in the browser shows /path/to/myFile.pdf%23Chapter3 instead of the desired /path/to/myFile.pdf#Chapter3.

How to escape the # in "filename=myFile#Chapter3"? Escaping with \ gives a compile-time error. Escaping with &035; doesn't work either.

ggkmath
  • 4,188
  • 23
  • 72
  • 129
  • Have you tried `\\#`? – Rogue Apr 08 '14 at 22:29
  • 1
    That's the correct thing for it to show in an address bar. Why are you concerned with a browser URL, shouldn't you be worried about a 'Save As' dialog? – bmargulies Apr 08 '14 at 22:33
  • 1
    @bmargulies that's a header, though, not an address bar (and why does it show as a URL at all I wonder?) – fge Apr 08 '14 at 22:33
  • I'm not sure what you're doing is valid. It's a filename, not a URL: it doesn't have a fragment part. – user207421 Apr 08 '14 at 22:36
  • Using \\# is replaced with \@23 in the URL, and doesn't work to advance the document to the named destination. Why do I worry about the browser's URL? When the URL appears as `/path/to/myFile.pdf%23nameddest=Chapter3` the PDF file opens to page 1. If I manually replace the %23 with # in the URL and press return, the PDF advances to Chapter 3 page. I'm trying to download the PDF file and have it open to the location specified by the named destination. The # character tells the PDF reader that the named destination follows. If the # sign isn't in the URL, the named destination is ignored. – ggkmath Apr 08 '14 at 22:39
  • @EJP, see answer provided here, regarding the fragment part. http://stackoverflow.com/questions/446614/how-can-i-programmaticly-open-a-pdf-at-a-certain-point – ggkmath Apr 08 '14 at 22:40
  • 1
    @ggkmath That link is about URLs. This question is about the filename in the Content-disposition header. It's not relevant. – user207421 Apr 08 '14 at 22:47
  • Got it. It just so happens this name shows up in the URL address, causing my confusion. – ggkmath Apr 08 '14 at 22:49
  • Leave out the #Chapter3. The purpose of putting a filename in Content-Disposition is to give the user a default filename to save this as. You can't save files with a # in the filename. – david brainerd Apr 09 '14 at 04:43

1 Answers1

3

RFC 2616 defines that "the Content-Disposition response-header field has been proposed as a means for the origin server to suggest a default filename if the user requests that the content is saved to a file" so I don't think you can do what you intend to through your servlet. Maybe you will have better luck with some script in the pdf : you could imagine parsing its own name to dynamically set it at the right anchor at opening.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • Perhaps it is a filename. I was hoping since it shows up in the URL that adding the #nameddest=... would implement advancing to the PDF named destination. Perhaps that's wishful thinking. – ggkmath Apr 08 '14 at 22:45
  • 1
    There's no 'perhaps' about it. It is a filename. – user207421 Apr 08 '14 at 22:49