我正在使用下面的代码来流式传输MemoryStream对象中的pptx,但是当我打开它时,我在PowerPoint中收到修复消息,什么是将MemoryStream写入响应对象的正确方式?

HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.pptx;", getLegalFileName(CurrentPresentation.Presentation_NM)));                
response.BinaryWrite(masterPresentation.ToArray());
response.End();

推荐答案

我也有同样的问题,唯一有效的解决方案是:

Response.Clear();
Response.ContentType = "Application/msword";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
Response.BinaryWrite(myMemoryStream.ToArray());
// myMemoryStream.WriteTo(Response.OutputStream); //works too
Response.Flush();
Response.Close();
Response.End();

Asp.net相关问答推荐

如何将标头中的用户名/密码传递给 SOAP WCF 服务

检测 ASP.NET 中的内存泄漏

IIS 中的嵌套 ASP.NET应用程序继承父配置值?

系统日期时间?与 System.DateTime

在 ASP.NET 中访问没有值的查询字符串参数

如何在 ASP.NET Web API 中使用非线程安全的 async/await API 和模式?

每月的每一天

会话变量保存在哪里?

如何使用 asp.net 获取 html Select 的选定值

带有文件名的 ASP.net MVC4 WebApi 路由

MVC5 身份验证中的......与主域之间的信任关系失败

在生产中使用 LocalDb 是否正常?

C# 7 本地函数未按预期工作且未显示错误

由于文件路径太长,网站发布失败

ASP.net 中是否有 after Page_Load 事件

c# list 如何在两个值之间插入一个新值

ASP.NET:在 Response.Redirect(...) 之后代码会发生什么?

如何判断字符串的第一个字符是否是字母,C#中的任何字母

Page.IsPostBack 和 Page.IsCallBack 有什么区别?

Sys.WebForms.PageRequestManagerServerErrorException:处理服务器上的请求时发生未知错误.