Mat*_*ull 2 c# pdf asp.net webforms httpresponse
我正在使用第三方软件从html文档渲染PDF.我创建了一个小型测试项目,并使用我能够从目录中读取html文档的OnClick事件<asp:Button>并将其呈现为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();
Run Code Online (Sandbox Code Playgroud)
当我查看Chrome中的响应时,我可以看到正确设置了Content-Type:
内容处置:内联; 文件名= HtmlToPdf.pdf;
内容长度:101482
内容类型:应用程序/ PDF
我试图将上述内容转移到我当前的项目中.唯一的区别是,<asp:Button>我在DevExpress网格视图中使用自定义按钮而不是一个.我最初在回调面板中处理了自定义按钮单击,但未设置Content-Type.在Chrome中查看响应证明了这一点:
内容处置:内联; 文件名= 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();
Run Code Online (Sandbox Code Playgroud)
response.ContentType ="application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=" + yourFileName + ".pdf");
stream.WriteTo(response.OutputStream);
response.Flush();
response.End();
Run Code Online (Sandbox Code Playgroud)
希望它有效:)