0

I am trying to return a PowerPoint file asynchronously to the client from a Spring MVC @RequestMapping method using Apache POI's HSFL. However I can't get the file to download

Here's my code

@RequestMapping(value = "/downloadPPT", produces = "application/vnd.ms-powerpoint")
public @ResponseBody byte[] downloadPPT(HttpServletResponse response) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] bytes = null;
    HSLFSlideShow ppt = new HSLFSlideShow();

    // add first slide
    HSLFSlide s1 = ppt.createSlide();

    // add second slide
    HSLFSlide s2 = ppt.createSlide();

    // save changes in a file
    FileOutputStream out = new FileOutputStream("slideshow.ppt");

    ppt.write(outputStream);
    out.close();
    bytes = outputStream.toByteArray();

    return bytes;

}

My response seems to be a byte-version of the ppt file I created, however the user doesn't get prompted to download the file. What am I doing wrong here?

Amit
  • 30,756
  • 6
  • 57
  • 88
Clay Banks
  • 4,483
  • 15
  • 71
  • 143

1 Answers1

1

you do not need to return anything. just try

OutputStream os = response.getOutputStream();

ppt.write(os);

response.setContentType("application/vnd.ms-powerpoint");
response.setHeader("Content-Disposition", "attachment;filename=\"slideshow.ppt\"");

and inside finally

os.flush();

os.close();

Try submitting a form from UI, you will get the expected result.

Tasmine Rout
  • 181
  • 6
  • Thanks Tasmine, hitting my Controller method directly in Internet Explorer is the only method that forces the download. Otherwise the download never initiates. Any thoughts on this? – Clay Banks Mar 27 '17 at 04:35