在asp.net中打开docx

时间:2010-10-15 05:54:05

标签: c# .net asp.net

我需要在浏览器中打开docx文件。试过下面的代码。但是文件损坏时会发生错误。内容类型是否正确,也尝试使用内容类型application / msword。

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

当给出适当的内容类型时,代码适用于所有其他文件类型。问题只出在docx上。

2 个答案:

答案 0 :(得分:3)

你得到了什么错误?

您可以为docx尝试此MIME类型吗?

application/vnd.ms-xpsdocument

编辑:我可以这样工作:

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

答案 1 :(得分:0)

你看过压缩吗?

Docx是zip文件,如果gzip压缩在服务器上处于活动状态,它就不会弄乱数据(可能是双重压缩,或者客户端尝试从docx中提取数据,假设它已被IIS压缩)

这只是一个想法,我不是说它,但我在使用iis压缩和zip文件时遇到了一些麻烦,所以可能与docx有关。

ps:你试过xslx还是pptx?