Reputation: 69
So I have a servlet whose URL is like blah.do?params=xyz
and in the servlet my code is similar to
ServletOutputStream out = response.getOutputStream();
request.setAttribute("Content-Type","application/pdf");
request.setAttribute("Content-Disposition","attachment;filename=test.pdf");
byte[] bytes = SystemServer.getFileContents(fileId).getBytes();
request.setAttribute("Content-Length","" + bytes.length);
out.write(bytes, 0, bytes.length);
out.flush();
I use
window.open(url,"my file","someparams");
but chrome is opening the window as pure text and view source confirms that all that was outputted was
%PDF-1.4 %áéëÓ 2 0 obj ..... all contents....%%EOF
So how can I force it to come up as a pdf
Whats wierd is I used identical code to get images back to the browser and it works fine
Upvotes: 0
Views: 245
Reputation: 7705
You are setting several things as request attributes which should actually be set on the response.
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
response.addHeader("Content-Disposition","attachment;filename=test.pdf");
Upvotes: 0
Reputation: 2821
You need to set those attributes on response object instead of request.
Upvotes: 1