2

I have an app that generates a docx file base on user input. It uses Apache POI to generate the docx file and I can get the FileOutputStream from that, the document opens perfectly on a local machine when I write it to a file.

The webapp is using Dojo xhrPost to send the necessary data to the server to generate the document. What I am wondering is how I get the docx file to the client.

I know I can do it be creating a temp file and passing the location of that file to the client to download, but I would think there would be a way to do it by piping the FileOutputStream straight to the client, which would be much cleaner.

Any suggestions?

Ben
  • 20,737
  • 12
  • 71
  • 115
zclark
  • 813
  • 2
  • 10
  • 26

1 Answers1

0

The answer from Mr Shiny in this SO question has an example streaming an excel file, should be very similar for a docx:

How can I get an Input Stream from HSSFWorkbook Object

Except that a docx content type should, probably, be application/vnd.ms-word

Community
  • 1
  • 1
Ben
  • 20,737
  • 12
  • 71
  • 115
  • Thank you. Seems to be I'm doing that bit right at least, my problem seems to lie in how I am handling the response on the javascript side. As it is I can see the response as a bunch of binary but it's not doing anything with it. Will post a new question as it seems to be a different issue. Might be a Dojo problem. – zclark Apr 04 '11 at 23:56
  • @zclark. I think your main problem is going to be with the xhr request. You cannot stream back on an xhr request, it's a common ajax pitfall. Your best bet is to create a hidden iframe and set the iframe source to your docx streaming script. This _will_ prompt the user to download/save the stream – Ben Apr 05 '11 at 00:41
  • Many thanks. I went with the iframe option, which in dojo was just dojo.io.frame.send(options). Only problem that I ran into was getting the iframe to resolve, which I ended up just using iframe.cancel in the event that there had been a previous iframe download and I wanted to initiate another one. – zclark Apr 05 '11 at 01:57
  • @zclark. Glad you've worked it out. Thanks for the additional feedback on the procedure, I'm sure someone else will find it helpful – Ben Apr 05 '11 at 02:16