Re: SOLVED: Re: returning PDF via appendToResponse
Re: SOLVED: Re: returning PDF via appendToResponse
- Subject: Re: SOLVED: Re: returning PDF via appendToResponse
- From: "Daniele Corti" <email@hidden>
- Date: Fri, 14 Dec 2007 13:25:52 +0100
2007/12/13, Patrick Robinson <email@hidden>:
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");
}
it would be a good idea to set also the Content-Type and the Content-Lenght to help the browser to manage the download:
try (i assume selectedThing is a File)
res.setHeader(""
+ selectedThing.length(), "Content-Length");
res.setHeader(WOApplication.application().resourceManager().contentTypeForResourceNamed(
selectedThing.getName()), "Content-Type");
FileDownloadComponent's .html and .wod files are empty, so it doesn't
seem to matter whether or not you call super.appendToResponse().
I think it's better to not call it
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");
Does this bring some change in the workflow or is simply to have a better log?
- 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 (
>
> 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:
email@hidden
--
Daniele Corti
AIM: S0CR4TE5
Messenger:
email@hidden--
Computers are like air conditioners -- they stop working properly if you open
WINDOWS
--
What about the four lusers of the apocalypse? I nominate:
"advertising", "can't log in", "power switch" and "what backup?"
--Alistair Young
_______________________________________________
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