2

Adding an object to a ModelAndView in a Spring controller after forcing a download does not seem to work.

Code at Controller method

 ModelAndView view = new ModelAndView("");
 view.setViewName("pom-upload");
 view.addObject("uploadStatus", "Uploaded pom has been successfully processed!");
 response.setHeader("Content-Disposition", "attachment; filename=pom.xml");
 IOUtils.copy(inputStreamToDownload, response.getOutputStream());
 response.flushBuffer();
 return view;

I get the file downloaded successfully. But when I try to access the "uploadStatus" message in my JSP like

<c:out value="${uploadStatus}"></c:out>

or

div id="status-message" class="alert alert-success" role="alert">${uploadStatus}</div>

I do not get the message from ${uploadStatus}

What could the reason be and how would I fix this?

Prasanna
  • 2,593
  • 7
  • 39
  • 53

1 Answers1

2

Ok, it sounds like you want to display a message to the user after they have downloaded a file. A couple options.

  1. User clicks download link. This goes to success page. Success page uses Refresh header or javascript to initiate download. So success comes a little early.

  2. See Detect when browser receives file download for some ideas on detecting when the browser gets the download.

Community
  • 1
  • 1
Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152