HttpContext.Current.Response.AddHeader()不设置Content-Type头 [英] HttpContext.Current.Response.AddHeader() not setting Content-Type header

查看:3472
本文介绍了HttpContext.Current.Response.AddHeader()不设置Content-Type头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用第三方软件来呈现从HTML文档的PDF文件。我创建了一个小的测试项目,并使用的的OnClick 事件的< ASP:按钮> 我能读从目录的html文件,并使其作为一个没有问题的PDF



的响应创建如下:

  HttpContext.Current.Response.Clear(); 
HttpContext.Current.Response.AddHeader(内容类型,应用程序/ PDF格式);
HttpContext.Current.Response.AddHeader(内容处置,
的String.Format({0};文件名=的test.pdf;,内联));
HttpContext.Current.Response.BinaryWrite(pdfBuffer);
HttpContext.Current.ApplicationInstance.CompleteRequest();

当我看着在Chrome的反应,我可以看到,内容类型被设置正确:




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



内容长度:101482



内容类型:应用程序/ PDF




我试过上面转移到我的当前项目。唯一的区别是,而不是一个< ASP:按钮> 我使用的DevExpress网格视图内的自定义按钮。我最初处理的自定义按钮点击在回调面板,但内容类型没有被设置的。在Chrome中浏览的响应证明了这一点:




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



内容编码:gzip



内容长度:149015



的Content-Type:text / plain的;字符集= UTF-8




我已经使用也试过gvDocuments.CustomButtonCallback + =新ASPxGridViewCustomButtonCallbackEventHandler(gvDocuments_CustomButtonCallback ); 事件,但内容类型仍然没有被设置



任何人有任何想法,为什么我不能在设置内容类型上面的场景?


解决方案

您可以尝试

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




response.ContentType =应用程序/ PDF格式;




  response.AddHeader(内容处置,附件;文件名=+ yourFileName +.PDF ); 
stream.WriteTo(response.OutputStream);
response.Flush();
到Response.End();



希望工程:)


I'm using third-party software to render PDFs from html documents. I created a small test project and using the OnClick event of an <asp:Button> I was able to read a html document from a directory and render it as a PDF with no problems.

The response is created as follows:

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

When I look at the response in Chrome, I can see that Content-Type is being set correctly:

Content-Disposition:inline; filename=HtmlToPdf.pdf;

Content-Length:101482

Content-Type:application/pdf

I've tried to transfer the above to my current project. The only difference being that instead of one <asp:Button> I'm using a custom button inside a DevExpress grid view. I Initially handled the custom button click in a callback panel, but Content-Type was not being set. Viewing the response in Chrome proved this:

Content-Disposition:inline; filename=5.pdf;

Content-Encoding:gzip

Content-Length:149015

Content-Type:text/plain; charset=utf-8

I've also tried using the gvDocuments.CustomButtonCallback += new ASPxGridViewCustomButtonCallbackEventHandler(gvDocuments_CustomButtonCallback); event but Content-Type is still not being set.

Anyone have any ideas as to why I cannot set Content-Type in the above scenario?

解决方案

You can try

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

hope it works :)

这篇关于HttpContext.Current.Response.AddHeader()不设置Content-Type头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆