-2

I have a problem about using Struts to download a file. I know Struts is an old technology, but my company is maintaining an old application for a customer.

So, problem is that we want a webpage to download a file. Scheme is the following : Reach for webpage > click a button > execute treatment (DB query and data selection) > produce excel file > download the file > back to initial page.

My code is working until the file download, i.e. I can download a correct excel file, but I can't go back to the original webpage.

I read this answer and others in this forum, and it seems that downloading a file and going back to original page are 2 different treatments, and that Struts can only process one...

I absolutely need those 2 treatments, so my idea is the following : When the user clicks the button on the original page, I should open another window (or even a pop-up) which will take care of the excel treatment and download, and my original page won't move, therefore I don't need to forward at the end of the treatment.

Question is, do you think this is viable ? I couldn't find a similar code here so I guess it isn't a good idea, so if you have a good practice about my need, please share it :)

Heratom
  • 35
  • 12

1 Answers1

0

Yes sure your idea will work. But the user has to close the pop-up after the download starts.

Just to give some other ideas: I had a similar problem once and solved it a little bit different. Javascript does allow execution of multiple statements in one row with one button (what you would do anyway with the popup and redirect solution).

So you could listen on the click event on the button and download the file and do something different after that.

Here are some topics about downloading files with javascript Link without using a popup. And a simple javascript redirect is also very easy Link #2.

The glue code to struts are two links you have the generate while the user visits your page.

More or less pseudo-code in html/jquery:

<button id="download" data-downloadlink="<s:property value='downloadlink'/>" data-redirectpage="<s:property value='redirectpage'/>">Download</button>
$('#downloadbutton').on('click', function() {
    download($('#downloadbutton').data('downloadlink'));
    redirect($('#downloadbutton').data('redirectpage'));
});
beendr
  • 654
  • 7
  • 21
  • Thanks a lot, this is indeed more graceful than the use of a pop-up. I'm gonna have a look at how to set this up, but my first concern is about the excel treatment that takes place before the download. If I chain the download right after I launched the treatment, won't the download happen too quickly ? I think I need to poll the treatment and wait for it to terminate before enabling the download, right ? Thanks for your help ! – Heratom Jan 08 '18 at 13:24
  • Maybe. I don't know exactly if browsers will wait until the file download starts. But you could test this easily outside of your app with something like jsfiddle. You could also use a `window.setTimeout()` to redirect after some time. – beendr Jan 08 '18 at 13:56
  • Thanks again @beendr, your answer and comment helped me solve my problem ;) For the record, I confirm that Javascript does NOT wait for the treatment to end, therefore I used a `setTimeout(reload, 5000)` before reloading the page. Also, I don't redirect to another page, my aim was to have the current page to be refreshed, therefore I use `location.reload(true)` to force the server to reload the page. – Heratom Jan 08 '18 at 16:17