HttpContext.Current.Response.AddHeader()未设置Content-Type标头

我正在使用第三方软件从html文档中呈现PDF。 我创建了一个小测试项目并使用OnClick事件,我能够从目录中读取一个html文档并将其呈现为PDF而没有任何问题。

响应创建如下:

 HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=Test.pdf;", "inline" )); HttpContext.Current.Response.BinaryWrite(pdfBuffer); HttpContext.Current.ApplicationInstance.CompleteRequest(); 

当我查看Chrome中的响应时,我可以看到正确设置了Content-Type:

内容处置:内联; 文件名= HtmlToPdf.pdf;

内容长度:101482

内容类型:应用程序/ PDF

我试图将上述内容转移到我当前的项目中。 唯一的区别是,而不是一个我在DevExpress网格视图中使用自定义按钮。 我最初在回调面板中处理了自定义按钮单击,但未设置Content-Type。 在Chrome中查看响应certificate了这一点:

内容处置:内联; 文件名= 5.pdf;

内容编码:gzip

内容长度:149015

内容类型:text / plain的; 字符集= utf-8的

我也尝试过使用gvDocuments.CustomButtonCallback += new ASPxGridViewCustomButtonCallbackEventHandler(gvDocuments_CustomButtonCallback); 事件但仍未设置Content-Type。

任何人都有任何想法,为什么我不能在上述场景中设置Content-Type?

你可以试试

 HttpResponse response = HttpContext.Current.Response; response.ClearContent(); response.Clear(); 

response.ContentType =“application / pdf”;

 response.AddHeader("Content-Disposition", "attachment; filename=" + yourFileName + ".pdf"); stream.WriteTo(response.OutputStream); response.Flush(); response.End(); 

希望它有效:)

 Dim Resp As HttpResponse = HttpContext.Current.Response Resp.Headers.Remove("X-Frame-Options") Resp.AppendHeader("X-Frame-Options", "SAMEORIGIN") 

通过在页面中添加上面的行,load / init方法将添加http响应头。