C#: How to display a PDF in a new Browser?

Jul 19 2018 1:29 AM
Hi Teams,
 
I'm here again to ask a question about my project.
I have a class where the PDF will be displayed:
public class FileDownload : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
bool IHttpHandler.IsReusable => true;
public void ProcessRequest(HttpContext context)
{
var path = context.Request.QueryString["path"];
//if(File.Exists(path + "\\PDF"))
if (File.Exists(path))
{
try
{
string filePath = path;
var file = Path.GetFileName(filePath);
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
context.Response.TransmitFile(filePath);
context.Response.Flush();
}
catch { }
}
}
}
 
and in JavaScript I have the corresponding Event Handler "window.open()" so that a new tab will be opened after I click the button to see the pdf:
 
window.open("FileDownload.ashx?path=" + response.d, "\\PDF", "_blank");
 
I have a problem with that, because when I click the button, the new window will be opened but: 1) the browser adress stays empty and
2) the pdf will be only downloaded
3) I first have to click on the pdf so that I can see the content of the pdf
 
 My goal is to open the PDF directly in the new browser (with ist address/url).
Does it depend on the TransmitFile() in C#?
 
I really hope you can help me.
Many Thanks
Best regards 
 

Answers (3)