Ondrej Tokar
Ondrej Tokar

Reputation: 5070

How to let user download a pdf file right after he visits a jsp page?

Simple question, I've got a pdf generated on page load, subsequently I want to let the user download the file.

Is there a way to do it? To show the download dialog after pdf was created?

I've tried this:

<%response.setHeader("Content-Disposition", "attachment; filename=\""+filename+"\"");%>

But where do I define, which file do I want user to download? Because code above leads to user downloading some corrupted empty pdf file.

Upvotes: 2

Views: 1512

Answers (1)

Bogdan
Bogdan

Reputation: 24580

I suggest you do this with a Servlet instead of a JSP. You have more control over the code and it is less fragile (if you handle the response content in the JSP and you have even a single space or line feed between your scriptlets, that can be written to the output stream corrupting the file content).

So have a look at this FileServlet example and adapt it to your needs.

Upvotes: 1

Related Questions