C#文件导出问题

我使用此代码导出/打开文件(pdf / xls / doc)。

Response.Clear(); Response.Buffer = true; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.Charset = "UTF-8"; Response.ContentType = mimeType; Response.AppendHeader("content-disposition", "inline; filename=" + fileName + "." + extension); Response.BinaryWrite(bytes); Response.End(); 

当我用Word打开一个doc文件并单击导出到xls或pdf时,在关闭Word之前没有任何反应。 关闭Word后,我已经能够打开xls和pdf了。

使用Excel打开xls时存在此问题。

是什么原因?

根据您使用的单词版本,我会说问题来自inline content-disposition

在Word的最新版本(在Word 2010中看到)中,它尝试WEBDAV inline时以WEBDAV模式打开文档,这可能会导致问题(锁定,缺少cookie或凭据尝试导出/打印文档时)

您可以查看网络流量以查看是否涉及此WEBDAV行为,并查看content-disposition attachment解决了问题。

希望这会有所帮助

原因是因为Word和其他类似Word的程序会锁定打开的文件以避免再次打开。 因此无法再从您的应用程序打开锁定的文件。