HttpContext.Current.Response.AddHeader() not setting Content-Type header(HttpContext.Current.Response.AddHeader() 未设置 Content-Type 标头)
问题描述
我正在使用第三方软件从 html 文档呈现 PDF.我创建了一个小型测试项目,并使用 <asp:Button>
的 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 中查看响应证明了这一点:
内容处理:内联;文件名=5.pdf;
内容编码:gzip
内容长度:149015
内容类型:文本/普通;字符集=utf-8
我也尝试过使用 gvDocuments.CustomButtonCallback += new ASPxGridViewCustomButtonCallbackEventHandler(gvDocuments_CustomButtonCallback);
事件,但仍未设置 Content-Type.
有人对为什么我不能在上述情况下设置 Content-Type 有任何想法吗?
你可以试试
HttpResponse response = HttpContext.Current.Response;response.ClearContent();响应.清除();
<块引用>
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=" + 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 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!