2014-02-20 372 views
0

显示我用一个处理程序来获取和使用下面的代码显示在浏览器窗口中的PDF:PDF不会在浏览器窗口中

byte[] byt = RetrieveDocument(int.Parse(context.Request.Params["id"]), context.Request.Params["title"]); 
string file = WriteDocumentFilePDF(byt); 
HttpContext.Current.Response.ContentType = "application/pdf"; 
HttpContext.Current.Response.AddHeader("content-length", byt.Length.ToString()); 
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=programdetails.pdf"); 
HttpContext.Current.Response.BinaryWrite(byt); 
HttpContext.Current.Response.End(); 

功能WriteDocumentFilePDF成功的PDF写入temp目录。我有上面的代码在不同的应用程序中正常工作。我错过了什么吗?

回答

0

如果您先通过byte[]memorystream,它会有所帮助吗?因此,像

byte[] byt = RetrieveDocument(int.Parse(context.Request.Params["id"]), context.Request.Params["title"]); 
string file = WriteDocumentFilePDF(byt); 
MemoryStream ms = new MemoryStream(byt); 

,然后添加你的头

HttpContext.Current.Response.ContentType = "application/pdf";  
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=programdetails.pdf"); 
HttpContext.Current.Response.BinaryWrite(ms.ToArray()); 
HttpContext.Current.Response.End(); 
2

当调试这样的问题,我觉得是小提琴手一个宝贵的工具;许多次它使我从简单的错误中解救出来。此外,本网站http://www.c-sharpcorner.com/uploadfile/prathore/what-is-an-ashx-file-handler-or-web-handler/举例说明了使用GIF图像做同样的事情。你的例子和他的区别似乎是使用Response.WriteFile()而不是使用BinaryWrite()直接写入Response。

我会在设置内容类型之前执行Response.ClearHeaders(),然后我将删除对Response.End()的调用。

相关问题