SOLVED: Re: returning PDF via appendToResponse
SOLVED: Re: returning PDF via appendToResponse
- Subject: SOLVED: Re: returning PDF via appendToResponse
- From: Patrick Robinson <email@hidden>
- Date: Thu, 13 Dec 2007 17:34:01 -0500
Well, it's always fun to reply to one's own emails.
First, the solution to (1) is to have the action method return a
different page, e.g.
public WOComponent downloadFile() {
WOComponent fileComponent = pageWithName("FileDownloadComponent");
fileComponent.setSelectedThing(aThing);
return fileComponent;
}
Then in FileDownloadComponent.appendToResponse(), you return the PDF:
public void appendToResponse( ... ) {
res.setHeader(mimeType, "content-type");
res.setHeader("attachment; filename=" + filename, "content-
disposition");
res.setContent(selectedThing.data());
res.disableClientCaching();
res.removeHeadersForKey("Cache-Control");
res.removeHeadersForKey("pragma");
}
FileDownloadComponent's .html and .wod files are empty, so it doesn't
seem to matter whether or not you call super.appendToResponse().
The result of doing it this way is that when I log the WOResponse in
Application.dispatchRequest(), all I see is the PDF file response
that I set, above. I can click the link to invoke the action method
repeatedly, but I only ever see the one WOResponse, now.
The solution to (2) is simply to change the content-disposition from
"attachment" to "inline". That is:
res.setHeader("inline; filename=" + filename, "content-
disposition");
- Patrick
On Dec 13, 2007, at 3:18 PM, Patrick Robinson wrote:
I've seen lots of examples for returning a PDF by fiddling with the
WOResponse in a component's appendToResponse() method. Code like
this:
public void appendToResponse(WOResponse res, WOContext con) {
super.appendToResponse(res, con);
if (someCondition) {
res.setContent(pdfFile);
res.setHeader("application/pdf", "Content-Type");
res.setHeader("attachment; filename=MyFile.pdf",
"Content-disposition");
res.disableClientCaching();
res.removeHeadersForKey("Cache-Control");
res.removeHeadersForKey("pragma");
}
}
We've made use of this kind of thing by having an action method
that sets "someCondition", as well as the details of what pdfFile
to download:
public void awake() {
someCondition = false;
}
public WOComponent downloadFile() {
someCondition = true;
pdfFile = nsDataForSelectedFile(selectedFile);
}
I have two problems:
(1) I was getting the PDF downloaded only every other time I
clicked the link bound to the downloadFile() action method.
Because I was curious, I implemented Application.dispatchRequest(),
and discovered that the first time I clicked the link, I'd get a
PDF file in the WOResponse. The next time I clicked it, the
WOResponse would contain the HTML of the page where the link
lived! Then, the next time, I'd get the pdf again. Why is this?
(2) I'd like to get the pdf to open in the browser, in the Acrobat
plugin (if there is one), rather than downloading it to the
downloaded file dir. I've read things suggesting that:
- you just need to change the content-disposition from "attachment"
to "inline".
- you may also need to specify "; name=MyFile.pdf" after the
"application/pdf" content-type.
- you may or may not need to specify the content-length.
- you shouldn't be calling super.appendToResponse(). I don't
understand how this would make a difference, as I'm replacing the
response's content, anyway.
But none of these things seems to work. Just changing the content-
disposition to inline has the effect of making the browser window
go black for a sec, then back to the HTML page (making me think
maybe I was on the right track).
At this point, I discovered problem (1), and wondered if the way
I'm mangling the WOResponse has something to do with it.
Anyone have any insights? I'd sure appreciate it!
--
Patrick Robinson
AHNR Info Technology, Virginia Tech
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
--
Patrick Robinson
AHNR Info Technology, Virginia Tech
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden