Aspose.Word Java to browser

Hello,

I have a web app where you enter some data on the form page, then click a submit button and then the server side code should create a word file based on information from the form page. Now I can get my code to produce a word (.docx) file if I do a save to c:\test.docx but I'm having trouble trying to stream this file back to the end user so they could get the open/save dialog box. Below is some test code trying to get the stream to work.

Code:

public void setLicense() throws Exception {
com.aspose.words.License license = new com.aspose.words.License();
license.setLicense("C:\\projects\\poitest\\lib\\Aspose.Words.lic");

}

public void doDoc(ActionEvent event) {

System.out.println("in the doDoc event *******-***-*-*-*-*-*");

try {

FacesContext context = FacesContext.getCurrentInstance();

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

this.setLicense();

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.write("hello world");

ServletOutputStream out = response.getOutputStream();

response.setHeader("Cache-Control", "max-age=30");

response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");

response.setHeader("Content-Disposition", "inline;filename=helloworld.docx");

doc.save(out, SaveFormat.DOCX);

context.responseComplete();

}

Results:

Page returns with PK

I'm using Aspose.Words.jdk16.jar from the 11.1 release. I've tried 11.0, & 10.8 same result. I working on a windows XP box, with Tomcat 6.0.18, and jsf

Thanks in advance for the help.

Hi Dale,


Thanks for your inquiry.

You need to implement an HTTPServlet before you can send the document to the browser. You can find a demo example with full code in the demo folders that are included with the install file for Aspose.Words Java. This is located where you installed the java version, inside “\Demos\Aspose.Words.Demos.Web”.

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 will help.

Best Regards,

Thank you, I was able to solve this issue. It turns out I was using a a4j:commandbutton to fire my method to create the word file. Once I changed it to a h:commandbutton everything worked great.

thanks again.

-Dale

Hi Dale,


Thanks for your feedback. It is perfect that you managed to achieve what you were looking for. Please let us know any time you have any further queries.

Best Regards,