tags:

views:

78

answers:

2

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.

+1  A: 

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();
adrianos
Tried with 'application/vnd.ms-xpsdocument'. The same problem exists. Word 2007 opens but its says that the file is corrupt. The MIME type has been added in IIS MIME types also.
Sandhya
are there images in the file?
adrianos
I tried a document with no Images, only formatted text. The requirement is to support all types of word files.
Sandhya
Did the file without images work?
adrianos
No, it did not work
Sandhya
Tried the above code. Only Word opens, the file does not open. There is no error also. Am I missing out something, any IIS settings?
Sandhya
The file exists? The filename and path is correct? The file opens normally when opening through the file system?
adrianos
Yes, the file exists and the path is right. The same code is fine for other file types, the problem is only with docx. Through file system, it opens fine.
Sandhya
I'm running out of ideas. How about getting that code I posted above and putting it into a clean test page -(i.e. without any other code potentially interfering with it).
adrianos
Did try the test page and it worked. Now shall try to figure out where the problem actually is. Thanks..
Sandhya
OK, try commenting out the response.addheader line
adrianos
Removed the addheader and added the try -catch to Response.end and it worked. But why is it that this is a problem only for docx files . Is there an option to provide a display filename?
Sandhya
That's good. I don't know why a problem exists for only docx files. What do you mean by option to provide a display filename?
adrianos
Response.AddHeader("content-disposition", "inline;filename=" + DisplayFileName);
Sandhya
In the AddHeader, the filename to be displayed to the client user could be provided. Is there any option for this?
Sandhya
What is the filename and does it contain spaces
adrianos
It can contain spaces. The filename could be any valid filename in the filesystem.
Sandhya
Does it work if you include the addheader line and use a file without spaces in the filename?
adrianos
A: 

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 ?

olifozzy
We do have a problem in our production server where the docx file is shown as a compressed file
Sandhya
Try to disable IIS compression for docx and see if it solves the issue.
olifozzy