How to Export to Browser instead of Saving to Directory?
Posted: Fri May 15, 2009 4:59 pm
Hi. I have docx4j working well with my application, and I am quite pleased.
However I have a new requirement and can't seem to get it working.
The requirement:
When a user clicks on a button, they are prompted to either open the word file or browse to save it to their system.
What I'm doing:
1) Writing to a String
2) In my servlet, I'm using a PrintWriter to write to serlvet response
Result:
The Word file is opening, but with the xml structure in purple bubbles (see attachment), and the bytes of the jpegs written out as text. I'm wondering if I'm missing something small or going about this completely the wrong way. Any help would be greatly appreciated.
However I have a new requirement and can't seem to get it working.
The requirement:
When a user clicks on a button, they are prompted to either open the word file or browse to save it to their system.
What I'm doing:
1) Writing to a String
- Code: Select all
String wordXML = "";
StringWriter sw = new StringWriter();
org.docx4j.convert.out.xmlPackage.XmlPackage xmlPackage = new org.docx4j.convert.out.xmlPackage.XmlPackage(wordMLPackage);
org.docx4j.xmlPackage.Package pkg = xmlPackage.get();
JAXBContext jc = Context.jcXmlPackage;
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(pkg, sw);
wordXML = sw.toString();
2) In my servlet, I'm using a PrintWriter to write to serlvet response
- Code: Select all
PrintWriter pWriter = null;
try{
pWriter = response.getWriter();
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-Disposition","attachment; filename=" + "test" + ".doc");
response.setContentLength((int)wordString.length());
pWriter.print(wordString);
}
Result:
The Word file is opening, but with the xml structure in purple bubbles (see attachment), and the bytes of the jpegs written out as text. I'm wondering if I'm missing something small or going about this completely the wrong way. Any help would be greatly appreciated.