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.

9
  • Have you tried \\#? Commented Apr 8, 2014 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? Commented Apr 8, 2014 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?) Commented Apr 8, 2014 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. Commented Apr 8, 2014 at 22:36
  • 1
    @ggkmath That link is about URLs. This question is about the filename in the Content-disposition header. It's not relevant. Commented Apr 8, 2014 at 22:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
There's no 'perhaps' about it. It is a filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.