【发布时间】:2012-07-23 07:11:25
【问题描述】:
我尝试从服务器端下载 docx 文件。 我怎么了?
这是代码:
FileInfo file = new FileInfo(filepath);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Disposition", "attachment; filename = " + ((Button)sender).CommandName + ".docx");
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.Flush();
Response.Close();
Response.End();
【问题讨论】:
-
这是一个普通的 asp.net 文件吗?您使用的是哪个浏览器?它发生在不同的浏览器上吗?
-
我下载了但是在office打不开,我用的是chrome
-
一目了然很难判断出什么问题。 Docx 文件只是 zip 文件,因此您可以将文件重命名为 zip 文件并查看它是否打开。如果您在普通的 asp.net 中运行此代码,那么很可能会出现一些有趣的问题,即 .NET 添加到响应中的内容,这就是我在下面建议使用 ashx 处理程序的原因。这在很大程度上取决于您在 ASP.NET 中运行上述代码的哪个事件。
标签: asp.net download content-type