Sandhya
Sandhya

Reputation: 11

open docx in asp.net

I have a requirement of opening the docx file in the browser. Tried with the code below. But error occurs that the file is corrupt. Is the content type correct, tried with thecontent type application/msword also.

Response.AddHeader("content-disposition", "inline;filename=" + DisplayFileName); Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";//getContentType(filename); Response.WriteFile(fullpath); Response.End(); Response.Flush();

The code works for all other file types when the appropriate content type is given. The problem is only with docx.

Upvotes: 1

Views: 4715

Answers (2)

adrianos
adrianos

Reputation: 1561

What error did you get?

Can you try this MIME type for docx?

application/vnd.ms-xpsdocument

EDIT: I can get it working like this:

Response.ClearContent();
Response.ClearHeaders();

Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

string fileName = "C:\\Your\\File\\Name.docx"; //change to your file name and path

Response.WriteFile(fileName);
Response.End();

Upvotes: 3

oleveau
oleveau

Reputation: 198

Have you looked about compression ?

Docx are zip file, if the gzip compression is active on the server it can't mess around the data (maybe double compression or maybe the client tries to extract the data from the docx assuming it's been compressed by IIS)

It's just an idea i'm not saying it's that , but I had some trouble with iis compression and zip files so maybe it's relevant with docx.

ps : have you tried xslx or pptx ?

Upvotes: 0

Related Questions