Free Support Forum - aspose.com

Sending word document back to client has no extension

I use the following code to stream back to client.


response.setContentType(“application/ms-word”);

response.setHeader(“Content-Disposition”, “attachment; ConstructionSpecification.docx”);
int saveFormat = FileFormatUtil.extensionToSaveFormat(“DOCX”);
doc.save(outs, saveFormat);

and it works except the file has no extension so when I try to open it, it asks me what I want to open it with and I have to select word. Then it opens correctly in word. I need it to automatically open in word. Any help is much appreciated.

Hi David,


Thanks for your inquiry. Perhaps the output file have not .docx extension. 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”.

Please also check the Java Examples from here:
http://www.aspose.com/demos/java-components/aspose.words/java/default.aspx

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.DOCX);
I hope, this will help.

So I commented out my eccess code so it matches your code. but still no extension on the resultant file.


ServletOutputStream outs = null;
outs = response.getOutputStream();
response.setContentType(“application/ms-word”);
//response.setHeader(“Content-Disposition”, “attachment; ConstructionSpecification.docx”);
//int saveFormat = FileFormatUtil.extensionToSaveFormat(“DOCX”);
mydoc.save(outs, SaveFormat.DOCX);

Hi David,


Thanks for your inquiry. I suggest you please see the code example shared inside “\Demos\Aspose.Words.Demos.Web”.

You can send output file to client browser with file extension by using following code snippet. Please see the response.setHeader method. Hope this helps you. Please let us know if you have any more queries.

/

* Called from the web-app index page (because the POST method is chosen for the input form).

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

// Get the output format selected by the user.

String formatType = “DOC”;

try

{

// Open the stream. Read only access is enough for Aspose.Words to load a document.

InputStream stream = new FileInputStream(MYDir + “in.htm”);

// Load the entire document into memory.

com.aspose.words.Document doc = new com.aspose.words.Document(stream);

// You can close the stream now, it is no longer needed because the document is in memory.

stream.close();

// Once we have a document, we can save it to a file, stream or send to the client’s browser.

// We just send the document to the browser in this case.

sendToBrowser(doc, “AsposeWords”, formatType, true, response);

response.flushBuffer();

}

catch (Exception e)

{

throw new RuntimeException(e);

}

}

/

* Sends the document to the client’s browser.

*/

private void sendToBrowser(Document doc, String demoName, String formatType, boolean openNewWindow, HttpServletResponse response)

throws Exception

{

String extension = formatType;

String fileName = demoName + “.” + extension;

// Add the Response header

if(openNewWindow)

response.setHeader(“content-disposition”,“attachment; filename=” + fileName);

else

response.addHeader(“content-disposition”,“inline; filename=” + fileName);

response.setContentType(“application/msword”);

doc.save(response.getOutputStream(), SaveFormat.DOC);

}