3

I have the following struts def:

<result name="reportSuccessfullyDownloaded" type="stream">
  <param name="contentType">application/zip</param>
  <param name="contentDisposition">attachment;filename="%{filename}"</param>
</result>

It downloads a .zip file that can contain anywhere from 1-10 generated pdf reports.

What I would like to do is change the app to handle the following situations:

  • If all reports were generated use the "stream" result and download the .zip file. No redirect necessary.
  • If some reports were generated but others had errors, use the "stream" result and download the .zip file with the generated pdfs and then redirect to an error page where the reports that were not generated are listed.
  • If no reports were generate, skip the download and redirect to an error page where the reports that were not generated are listed.

My question is: how can I have more than one "result" for a single Action? I don't see how it's possible. Is it? Or do I have to somehow have to use a "chain" result, which is highly discouraged?

Any help much appreciated.

Robert Bowen
  • 487
  • 2
  • 13
  • 24
  • A "chain" won't do what you want either--you can either get data, or a redirect. Having both doesn't make any sense. You could show a page then maybe redirect to an attachment, maybe the browser wouldn't change pages then. Otherwise just have the page with a download link. – Dave Newton Oct 15 '12 at 15:58
  • Yea, I didn't think so. I just wanted to make sure. Unfortunately the client doesn't want to make the user click on a link, they want to download to start automatically. So I guess I'll just have to let it 'die silently' - remove reports with errors from the .zip and only include those that worked. Anyway, thanks for the advice. – Robert Bowen Oct 15 '12 at 22:18
  • Again, if you forward to a page w/ user-facing info on it, then that page redirects to an action w/ an attachment, the page might not forward. You could also use an iframe, or something like [this jquery download library](http://johnculviner.com/post/2012/03/22/Ajax-like-feature-rich-file-downloads-with-jQuery-File-Download.aspx). – Dave Newton Oct 15 '12 at 22:30
  • I was toying around with Ajax. But in the end I end up pretty much where I started. I can use Ajax to download the .zip file. But if, for example, 1 of the 5 PDFs in the .zip file didn't get generated correctly, I want to download the .zip file and inform the user that one report is missing. But the output of the Struts Action is a stream. I can't download the stream (the .zip) and the errors (if any) at the same time. Can I? – Robert Bowen Oct 16 '12 at 14:24
  • I'm not convinced you read through the jQuery thing I linked to; AFAIK you could use that, or that technique, to do precisely what you want: the initial call preps the files. On the return, you can report back whatever you need to, and the iframe starts the download. What am I missing? – Dave Newton Oct 16 '12 at 14:47
  • Perhaps I am not explaining myself properly. I have the jQuery download plugin working on my page. But I don't see how I can, on the one hand, "report back" whatever I need to (errors, info, whatever) and on the other, download the .zip file. My Action only has one possible Result, a stream. eg. the .zip file. So where would I stick my errors and other info? The Response has Content-Type: application/zip; and Content-Disposition: attachment;filename="reportXxxx.zip". It seems to be **me** who is missing something! :) – Robert Bowen Oct 16 '12 at 15:47
  • The ajax response would contain user-facing info. In the response handler you either do whatever it is the plugin uses, or you create an iframe dynamically, or set the URL of an existing one, w/ the action that does the download. The iframe does the download. I don't know how the plugin does that. – Dave Newton Oct 16 '12 at 16:05
  • In the end I chose to break up the process into 2 separate processes, something I didn't think I was allowed to do. First I generate and save the file. Then redirect to an info page with all of the "user-facing info" and a link to download. It's much more straight-forward this way. Many thanks for all of your help. The jQuery download plugin rocks. – Robert Bowen Oct 17 '12 at 15:52

1 Answers1

1

I know this is very old already but I would suggest you to store the status of the report generation in session, and have an ajax action that serves the status of generation, and another action to download the file.

In that way, you can update the status of the report generation multiple times and serve it nicely like:

  • January - Generated
  • February - Error #231, contact support
  • March - Generated
  • April - In progress ...
  • May - Pending

And once everything is done you can automatically start the download also. Just make sure to clear the session when the user re-enters the page or how ever seems appropriate.

Reigo
  • 285
  • 2
  • 9