We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Save to client browser

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?

Hi John,

Thanks for your inquiry. I think, you need to implement an HTTPServlet before you can send the document to the client's browser. You can find attached a demo example .

Using the servlet you can send the document to a browser like in the example code below which sends a .doc file to the browser.
response.setContentType("application/msword");
doc.save(response.getOutputStream(), SaveFormat.FORMAT_DOCUMENT);
I hope, this helps.

Best regards,