skip to Main Content

i can read the pdf and open it in the current tab however everything i have tried to open the pdf in a new tab is not working. I added javascript as seen in the commented out sections and yet it always opens in the same tab. Is it something i am missing or am i overthinking it

protected void bttnpdf_Click(object sender, EventArgs e)
{
    string fileName = null;
    fileName = "testPdf1.pdf";

    if (fileName != null)
    {
        string path = @"c:TestCodeTestPdfs" + fileName;
        if (File.Exists(path))
        {
            WebClient User = new WebClient();
            Byte[] FileBuffer = User.DownloadData(path);
            if (FileBuffer != null)
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('" + FileBuffer + "');", true);
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", FileBuffer.Length.ToString());
                Response.BinaryWrite(FileBuffer);
            //string script = string.Format("window.open('{0}','_blank');", FileBuffer);
            //ScriptManager.RegisterStartupScript(this, GetType(),
            //                      "ServerControlScript", script, true);
            //bttnpdf.SetClientScript(string.Format("window.open('{0}','_blank');", FileBuffer), false);
            }
        }
    }
}

2

Answers


  1. Set the target attribute of the link to "_blank" in your front-end code (Razor view). It’ll open the link in a new window.

    Login or Signup to reply.
  2. You can write the action in a separate ashx page to get the result, then just pass from the client side to the ashx page

    Only in place

    Response
    

    Use with

    context.Response
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search