Can I send the ms-word document to browser for user to download instead of saving as a file on the server?
I saw this question answered in the forums before but I cannot seem to find the correct demo source code and everything I try does not work.
Can you provide me with some guidance?
Thanks
I have two different scenarios. One will ask the user to save the file and one won’t.
1) The method that asks the user to save the file:
I create a Document object using the default constructor ().
Document doc = new Document();
I use DocumentBuilder to create the data for the document
DocumentBuilder builder = new DocumentBuilder(doc);
// Write a new paragraph in the document with the text “Hello World!”
builder.write(“Program/Project: " + “title”);
I then save the file
doc.save(“docs/” + “templateName.doc”);
This version asks the user to save the file.
In the second version I create a document using a mailmerge.doc that lives on the server.
Document doc2 = new Document(“docs/”+ “MailMergeTemplate.doc”);
doc2.getMailMerge().execute(
new String[] {“projectTitle”, “todaysDate”},
new Object[] {“my titl”, new Date() +”"});
Then I save it…
doc3.save(“docs/” + “MailMergeTemplateOut.doc”);
This way does not ask the user to save it - the file is created on the server in the docs folder.
Any guidance for me here?
response.setContentType("application/msword"); doc.save(response.getOutputStream(), SaveFormat.FORMAT_DOCUMENT);