1
votes

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.

2

2 Answers

3
votes

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();
0
votes

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 ?